Vertical text in datagridview

前端 未结 4 1861
孤城傲影
孤城傲影 2020-12-16 18:03

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

Thanks

4条回答
  •  眼角桃花
    2020-12-16 18:11

    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;
        }
    }
    

提交回复
热议问题