C#学习教程:DataGridView中的竖排文本我应该怎么办?谢谢您可以对标题使用自定义单元格绘画以获得所需的结果。为了回应您要求找到一种将文本与单元格底部对齐的方法的评论,我在我的代码中添加了评论。他们想说清楚。您需要以下代码(比如在初始化组后在Form_Load中)dataGridView1.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.EnableResizing;dataGridView1.ColumnHeadersHeight=50;dataGridView1.AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;//在这里,我们将事件处理程序附加到单元格绘制事件dataGridView1.CellPainting+=newDataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);接下来,您需要类似于以下的代码:voiddataGridView1_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){//检查我们是否在标题单元格中!如果(e.RowIndex==-1&&e.ColumnIndex>=0){e.PaintBackground(e.ClipBounds,true);矩形rect=this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex,true);大小titleSize=TextRenderer.MeasureText(e.Value.ToString(),e.CellStyle.Font);if(this.dataGridView1.ColumnHeadersHeight一个更简单、更高效的渲染器附加事件dataGridView1.CellPainting+=newDataGridView1_CellPainting(dataGridView1_CellPainting);绘制旋转文本的事件处理程序privatevoidDataGridView1_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){//如果要跳过第一列,则从第0列垂直文本,或在下方调整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);.Graphics.DrawString(e.FormattedValue.ToString(),e.CellStyle.Font,Brushes.Black,5,5);e.Graphics.ResetTransform();e.Handled=true;}}DataGridd=newDataGrid();d.Columns[0].HeaderStyle.VerticalAlign=VerticalAlign.Bottom;如果您正在寻找gridview,那么您的情况如下:-GridViewgv=newGridView();gv.Columns[0].ItemStyle.VerticalAlign=VerticalAlign.Bottom;如果您正在寻找datagridview那么您的情况如下:-创建datagridview对象以上就是《C#学习教程:datagridview中竖排文本分享》的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注---gv.Columns["ColumnName"].HeaderCell。Style.Alignment=DataGridViewContentAlignment.BottomCenter;voiddataGridView1_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){if(e.RowIndex==-1&&e.ColumnIndex>=0){e.PaintBackground(e.ClipBounds,true);矩形rect=this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex,true);大小titleSize=TextRenderer.MeasureText(e.Value.ToString(),e.CellStyle.Font);if(this.dataGridView1.ColumnHeadersHeight本文采集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
