【问题标题】:.NET DataGridView: Remove "current row" black triangle.NET DataGridView:删除“当前行”黑色三角形
【发布时间】:2011-08-15 01:39:17
【问题描述】:

在 DataGridView 中,即使您将网格设置为只读,在当前行显示的行标题处也会有一个黑色三角形。

我想避免显示它,也想避免三角形引起的那些单元格的大填充。我猜填充是由三角形引起的,因为单元格的填充是 0。

可以这样做吗?怎么样?

谢谢!

编辑

这就是行标题文本的创建方式:

for (int i = 0; i < 5; i++)
{
    DataGridViewRow row = new DataGridViewRow();
    row.HeaderCell.Value = headers[i];
    dataGridView1.Rows.Add(row);
}

headers 它是一个简单的字符串数组。 (string[])

【问题讨论】:

    标签: .net winforms datagridview


    【解决方案1】:
    • 如果您想保留行标题而不是隐藏它们,那么您可以使用单元格填充将三角形推到视线之外:

      this.dataGridView1.RowHeadersDefaultCellStyle.Padding = 
          new Padding(this.dataGridView1.RowHeadersWidth);
      
    • 如果您正在使用行标题文本并希望保持可见,您需要使用一些自定义绘画 - 谢天谢地非常简单。在上面的代码之后,只需附加到 RowPostPaint 事件,如下所示:

      dataGridView1.RowPostPaint += 
          new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
      

      并且在 RowPostPaint 方法中:

      void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
      {
          object o = dataGridView1.Rows[e.RowIndex].HeaderCell.Value;
      
          e.Graphics.DrawString(
              o != null ? o.ToString() : "",
              dataGridView1.Font, 
              Brushes.Black, 
              new PointF((float)e.RowBounds.Left + 2, (float)e.RowBounds.Top + 4));
      }
      

      正如 Dan Neely 指出的那样,使用上面的 Brushes.Black 会覆盖任何现有的更改,因此最好使用画笔:

      new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
      

    【讨论】:

    • 这对三角形很有用,但它也会移动标题文本,我想要文本而不是三角形,这可能吗?
    • @Diego 嗨,不确定行标题中的文字是什么意思。像乔一样,我从未见过这种情况。您能否使用创建此文本的代码编辑您的问题,我会看看。
    • @Diego 哇,只是向您展示了 DataGridView 中有多少选项 - 我已经使用它 并且从未见过 HeaderCell.Value 使用过。我在答案中添加了一些保留文本的代码。
    • @David Hall 您应该使用new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor) 来维护任何样式更改,而不是默认为黑色。后者也会在主题改变了 WindowsText 颜色的任何系统上引起问题。
    • 因为出现了两个相邻的字符串,所以我在e.Graphics.DrawString(...)之前添加了e.Graphics.FillRectangle(new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.BackColor), e.RowBounds.Left + 1, e.RowBounds.Top + 1, dataGridView1.Rows[e.RowIndex].HeaderCell.Size.Width - 2, dataGridView1.Rows[e.RowIndex].HeaderCell.Size.Height - 2);
    【解决方案2】:

    RowHeadersVisible 设置为false

    【讨论】:

    • @Jow White:我希望能够看到标题和标题文本,我只是不想要烦人的三角形。
    • 对不起,您的名字有错别字。
    • 我从未在行标题中看到过文本——只有三角形。您是否将行标题(在每行的左侧,带有灰色框和三角形)与列标题(每列顶部的名称)混淆了?
    • 不,我有一个复式表,所以我的行标题中有文本。我不确定复式表是否是正确的英文表达方式。
    【解决方案3】:

    一个非常简单的解决方案是将行高设置为 16 像素或更小。 这会禁用行标题单元格中的所有图标。

    dataGridView1.RowTemplate.Height = 16;
    

    【讨论】:

    • 这对我有用。我喜欢将行号放在行标题上。字形将数字向右推,所以我必须使行标题比我想要的宽。这是一个错误。 Microsoft 文档指出,如果两者都没有足够的空间,headercell 值会将字形推离屏幕。实际发生的情况恰恰相反:字形将 headercell.value 推开。并且禁用显示图标也不起作用
    • 此外,您必须禁止调整行大小。否则用户调整行大小,问题又回来了。
    【解决方案4】:

    三角问题,很简单,放

    dgv_Products.Rows[xval].Selected = true;
    dgv_Products.CurrentCell  = dgv_Products.Rows[xval].Cells[0];
    

    将当前单元格属性设置为当前选定行的单元格零。(已测试适用于 dgv_Products.MultiSelect = false ;)

    【讨论】:

    • 谢谢,多选随机行的问题已经消失了
    【解决方案5】:

    DataGridViewRowPostPaintEventArgs 包含这个特殊的 PaintHeader 方法:

    PaintHeader(DataGridViewPaintParts) - Paints the specified parts of the row header of the current row.
    

    这是 DataGridViewPaintParts 枚举: https://msdn.microsoft.com/en-us/library/ms159092%28v=vs.110%29.aspx

    所以你要做的是在你的 datagridview 的 RowPostPaint 事件中,首先告诉它只绘制行标题的背景......就像这样:

    e.PaintHeader(DataGridViewPaintParts.Background)
    

    然后告诉它绘制你想要的任何字符串。这是我的例子:

    Private Sub MyDGV_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles dgvData.RowPostPaint
    
        Dim grid As DataGridView = DirectCast(sender, DataGridView)
    
        e.PaintHeader(DataGridViewPaintParts.Background)
    
        Dim rowIdx As String = (e.RowIndex + 1).ToString()
    
        Dim rowFont As New System.Drawing.Font("Segoe UI", 9.0!, _
            System.Drawing.FontStyle.Bold, _
            System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    
        Dim centerFormat = New StringFormat()
        centerFormat.Alignment = StringAlignment.Far
        centerFormat.LineAlignment = StringAlignment.Near
    
        Dim headerBounds As Rectangle = New Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height)
    
        e.Graphics.DrawString(rowIdx, rowFont, SystemBrushes.ControlText, headerBounds, centerFormat)
    
    End Sub
    

    【讨论】:

    • 为每个绘图创建新字体是一个巨大的不必要的开销。
    【解决方案6】:

    如果有人仍然想知道:

    dataGridView1.RowHeadersWidth = 4; // the left row header size.
    

    这将去掉三角形并缩小默认大小。

    希望对您有所帮助。

    【讨论】:

      【解决方案7】:
      dataGridView1.CurrentCell = null;
      

      将完全移除黑色箭头。

      每次DataGridView 中的数据更新时都需要运行此行。

      【讨论】:

        【解决方案8】:

        尝试了其他答案,但我无法将相同的字体外观与列字体匹配。 我在Microsoft Forums找到了解决方案

        先订阅DataGridView.CellPainting点赞;

        dgv.CellPainting += Dgv_CellPainting;

        然后打印你想要的文本(原始帖子有行索引);

        private void Dgv_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
                {
                    if (e.ColumnIndex == -1 && e.RowIndex > -1)
                    {
                        object o = (sender as DataGridView).Rows[e.RowIndex].HeaderCell.Value;
                        e.PaintBackground(e.CellBounds, true);
        
                        using (SolidBrush br = new SolidBrush(Color.Black))
                        {
                            StringFormat sf = new StringFormat();
                            sf.Alignment = StringAlignment.Center;
                            sf.LineAlignment = StringAlignment.Center;
                            e.Graphics.DrawString(o.ToString(),
                            e.CellStyle.Font, br, e.CellBounds, sf);
                        }
                        e.Handled = true;
                    }
                }
        

        【讨论】:

          【解决方案9】:
           'Datagridview'.rowheadervisible=false 
          

          隐藏黑色箭头行选择器ma bob

          Datagridview 是您的数据网格的名称,不带引号 '' 所以.... 如果您的网格名为 Example 那么

           Example.rowheadervisible=false
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-11-08
            • 2017-04-29
            • 2014-01-21
            • 1970-01-01
            • 1970-01-01
            • 2013-08-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多