Vertical text in datagridview

淺唱寂寞╮ 提交于 2019-11-27 17:55:34

问题


I want to show the text in the header cells in vertical orientation. How can I do it?

Thanks


回答1:


You can achieve the result you want using custom cell painting for the header.

In answer to your comment asking for a way to align the text with the bottom of the cell, I've added comments to my code. They are hopefully clear.

You need the following code (say in the Form_Load after initializing components)

dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
dataGridView1.ColumnHeadersHeight = 50;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;

// Here we attach an event handler to the cell painting event
dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);

Next you need something like the following code:

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    // check that we are in a header cell!
    if (e.RowIndex == -1 && e.ColumnIndex >= 0)
    {
        e.PaintBackground(e.ClipBounds, true);
        Rectangle rect = this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
        Size titleSize = TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
        if (this.dataGridView1.ColumnHeadersHeight < titleSize.Width)
        {
            this.dataGridView1.ColumnHeadersHeight = titleSize.Width;
        }

        e.Graphics.TranslateTransform(0, titleSize.Width);
        e.Graphics.RotateTransform(-90.0F);

        // This is the key line for bottom alignment - we adjust the PointF based on the 
        // ColumnHeadersHeight minus the current text width. ColumnHeadersHeight is the
        // maximum of all the columns since we paint cells twice - though this fact
        // may not be true in all usages!   
        e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y - (dataGridView1.ColumnHeadersHeight - titleSize.Width) , rect.X));

        // The old line for comparison
        //e.Graphics.DrawString(e.Value.ToString(), this.Font, Brushes.Black, new PointF(rect.Y, rect.X));


        e.Graphics.RotateTransform(90.0F);
        e.Graphics.TranslateTransform(0, -titleSize.Width);
        e.Handled = true;
    }
}



回答2:


A simpler and more effective renderer

Attach event either through designer, or with this line of code

dataGridView1.CellPainting += new DataGridView1_CellPainting(dataGridView1_CellPainting);

Event handler to draw rotated text

private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
    // Vertical text from column 0, or adjust below, if first column(s) to be skipped
    if (e.RowIndex == -1 && e.ColumnIndex >= 0) {
        e.PaintBackground(e.CellBounds, true);
        e.Graphics.TranslateTransform(e.CellBounds.Left , e.CellBounds.Bottom);
        e.Graphics.RotateTransform(270);
        e.Graphics.DrawString(e.FormattedValue.ToString(),e.CellStyle.Font,Brushes.Black,5,5);
        e.Graphics.ResetTransform();
        e.Handled = true;
    }
}



回答3:


DataGrid d = new DataGrid();
d.Columns[0].HeaderStyle.VerticalAlign = VerticalAlign.Bottom;

If you are looking for gridview then you case like this :-

GridView gv = new GridView ();
gv.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;

If you are looking for datagridview then you case like this :- Create an object of datagridview.

gv.Columns["ColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter;



回答4:


void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
   {
           if (e.RowIndex == -1 && e.ColumnIndex >= 0)
           {
               e.PaintBackground(e.ClipBounds, true);
               Rectangle rect =
this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex, true);
               Size titleSize =
TextRenderer.MeasureText(e.Value.ToString(), e.CellStyle.Font);
               if (this.dataGridView1.ColumnHeadersHeight <
titleSize.Width)
                   this.dataGridView1.ColumnHeadersHeight =
titleSize.Width;

               e.Graphics.TranslateTransform(0, titleSize.Width);
               e.Graphics.RotateTransform(-90.0F);

               e.Graphics.DrawString(e.Value.ToString(), this.Font,
Brushes.Orange, new PointF(rect.Y, rect.X));

               e.Graphics.RotateTransform(90.0F);
               e.Graphics.TranslateTransform(0, -titleSize.Width);
               e.Handled = true;
           }
  }

In addition, you could set the AutoSizeColumnsMode property of the
DataGridView to AllCellsExceptHeader in order to make the DataGridView
compact.


来源:https://stackoverflow.com/questions/5782674/vertical-text-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!