• 将文本在单元格中垂直显示


    将文本在DataGridView的单元格中竖直显示,需要在CellPainting事件中设置StringFormatFlag.DirectionVertical

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication2
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();

    dt.Columns.Add("c1");

    dt.Columns.Add("c2");

    for (int j = 0; j < 10; j++)
    {

    dt.Rows.Add("aaaaaaaaa", "bbbb");

    }

    this.dataGridView1.DataSource = dt;



    this.dataGridView1.CellPainting += new

    DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting); ;


    }

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
    if (e.ColumnIndex == 1 && e.RowIndex > -1 && e.Value != null)
    {

    e.Paint(e.CellBounds, DataGridViewPaintParts.All

    & ~DataGridViewPaintParts.ContentForeground);

    StringFormat sf = new StringFormat();

    sf.Alignment = StringAlignment.Center;

    sf.LineAlignment = StringAlignment.Center;

    sf.FormatFlags = StringFormatFlags.DirectionVertical;

    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

    new SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf);

    e.Handled = true;

    }


    }







    }
    }

  • 相关阅读:
    Unity3d通用工具类之生成文件的MD5
    Unity3d-Socket之龙一编年史network.dll分析(2)-> CNetLogger
    Unity3d-Socket之龙一编年史network.dll分析(1)
    Unity3d设计模式之单例模式
    c#中的??运算符
    java 字节流
    java File文件操作
    java 线程池和lamda表达式
    java 线程状态(通信/等待/唤醒)
    java 线程安全
  • 原文地址:https://www.cnblogs.com/because/p/2295115.html
Copyright © 2020-2023  润新知