zoukankan      html  css  js  c++  java
  • 将文本在单元格中垂直显示

    将文本在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;

    }


    }







    }
    }

  • 相关阅读:
    LightOJ 1139 8 puzzle + hdu 1043 Eight A*
    hdu 1180 优先队列 + bfs
    hdu 1270
    HDU Doing Homework
    hdu 1171 Big Event in HDU
    hdu 3613 (KMP)回文串
    POJ 3461 Oulipo(KMP)
    POJ 1565(DP状态压缩)
    NYOJ 634 万里挑一(优先队列)
    职场手记1_你想成文什么样的人
  • 原文地址:https://www.cnblogs.com/because/p/2295115.html
Copyright © 2011-2022 走看看