Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# datagridview中的垂直文本_C#_.net_Winforms_Datagridview_Vertical Text - Fatal编程技术网

C# datagridview中的垂直文本

C# datagridview中的垂直文本,c#,.net,winforms,datagridview,vertical-text,C#,.net,Winforms,Datagridview,Vertical Text,我想以垂直方向显示标题单元格中的文本。我怎么做 谢谢 如果您正在寻找gridview,那么您的案例如下:- GridView gv = new GridView (); gv.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom; 如果您正在寻找datagridview,那么您可以这样做:- 创建datagridview的对象 gv.Columns["ColumnName"].HeaderCell.Style.Alignment =

我想以垂直方向显示标题单元格中的文本。我怎么做

谢谢

如果您正在寻找gridview,那么您的案例如下:-

GridView gv = new GridView ();
gv.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Bottom;
如果您正在寻找datagridview,那么您可以这样做:- 创建datagridview的对象

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

可以使用标题的自定义单元绘制来实现所需的结果

在回答您关于如何将文本与单元格底部对齐的问题时,我在代码中添加了注释。希望它们是清楚的

您需要以下代码(例如,在初始化组件后加载)

接下来,您需要类似以下代码的代码:

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;
    }
}
void dataGridView1\u CellPaint(对象发送方,DataGridViewCellPaintingEventArgs e)
{
//检查我们是否在标题单元格中!
如果(e.RowIndex=-1&&e.ColumnIndex>=0)
{
e、 绘画背景(如剪贴画,真实);
Rectangle rect=this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex,true);
Size titleSize=TextRenderer.MeasureText(e.Value.ToString(),e.CellStyle.Font);
if(this.dataGridView1.columnHeadershight
无效dataGridView1\u单元绘制(对象发送方,DataGridViewCellPaintingEventArgs e)
{
如果(e.RowIndex=-1&&e.ColumnIndex>=0)
{
e、 绘画背景(如剪贴画,真实);
矩形矩形=
this.dataGridView1.GetColumnDisplayRectangle(e.ColumnIndex,true);
大小标题化=
textrender.MeasureText(e.Value.ToString(),e.CellStyle.Font);
if(this.dataGridView1.columnHeadershight<
标题大小(宽度)
this.dataGridView1.columnHeadershight=
标题化。宽度;
e、 图形。TranslateTransform(0,标题大小。宽度);
e、 图形。旋转变换(-90.0F);
e、 Graphics.DrawString(例如Value.ToString(),this.Font,
橙色,新的点F(矩形Y,矩形X));
e、 图形.旋转变换(90.0F);
e、 图形.平移格式(0,-标题大小.宽度);
e、 已处理=正确;
}
}
此外,还可以设置
将DataGridView设置为AllCellsCeptheader,以使DataGridView
契约

更简单、更有效的渲染器

通过设计器或使用此行代码附加事件

dataGridView1.CellPainting += new DataGridView1_CellPainting(dataGridView1_CellPainting);
用于绘制旋转文本的事件处理程序

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

@TechGiant:DataGridView的第二行抛出2个错误:“System.Windows.Forms.DataGridViewColumn”不包含“ItemStyle”的定义,并且找不到接受“System.Windows.Forms.DataGridViewColumn”类型的第一个参数的扩展方法“ItemStyle”(是否缺少using指令或程序集引用)和:当前文件中不存在名称“VerticalAlign”context@Diego-对于datagridview,我写了最后一行。试试最后一行,它会给出你的答案result@TechGiant:也不编译:非invocable成员'System.Windows.Forms.DataGridView.Columns'不能像方法一样使用。现在请尝试最后一行实际需要使用的像gv.Columns[“ColumnName”]而不是gv.Columns(“ColumnName”)…我已经试过了…@Diego-我可以知道为什么要使标题文本垂直吗?尽管我通常对更改datagridview的标准行为提出警告-请检查这是否是必须的。控制可以做很多事情,但是经常会有意想不到的副作用,会让他们头昏脑胀。谢谢!!您现在知道如何更改此代码以使文本与单元格底部对齐了吗?@Diego我添加了与单元格底部对齐的代码,不过,请再次进行彻底测试,因为它使用了单元格绘制对标题触发两次的事实,这可能并不总是正确的。@DavidHall这是一种使所有单元格垂直显示的方法吗?不仅仅是列标题。所有单元格都垂直显示。非常感谢,我可以使用这个。我应该添加两个注释:1-使用e.CellBounds而不是这个.dataGridView1.GetColumnDisplayRectangle(…)。原因是它更快,而且由于某些原因,当我访问dataGridView1时,我会得到视觉效果!2-为提高性能,启用双缓冲(请参阅)
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.
dataGridView1.CellPainting += new DataGridView1_CellPainting(dataGridView1_CellPainting);
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;
    }
}