.NET DataGridView:删除“当前行”黑色三角形

问题描述 投票:0回答:11

在 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
11个回答
20
投票
  • 如果您想保留行标题而不是隐藏它们,那么您可以使用单元格填充将三角形推到看不见的地方:

    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)
    

8
投票

RowHeadersVisible
设置为
false


5
投票

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

dataGridView1.RowTemplate.Height = 16;

2
投票

三角形问题,很简单,放

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

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


2
投票
dataGridView1.CurrentCell = null;

将完全删除黑色箭头。

每次

DataGridView
中的数据更新时,您都需要运行此行。


2
投票

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

1
投票

如果有人还想知道:

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

这将消除三角形并缩小默认尺寸。

希望有帮助。


0
投票

将 RowHeadersVisible 设置为 false,并在前面添加一列。似乎是最简单的方法


0
投票

David Halls 答案已修改为居中对齐文本

// Subscribe to the post paint event
dgv.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgv_RowPostPaint);

// Push the triangle (and text) off the cell
dgv.RowHeadersDefaultCellStyle.Padding = new Padding(this.dgv.RowHeadersWidth);

// Each row has its header cell text measured. The width of the row header and the width
// of the text are calculated to form an offset to push the text right (or left if the 
// text is to wide!) off of the cell boundary left edge
void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
string text = dgv.Rows[e.RowIndex].HeaderCell.Value.ToString();

Size textSize = TextRenderer.MeasureText(text, dgv.Rows[e.RowIndex].Cells[0].InheritedStyle.Font);

float fudgeOffset = 1.0F;

float offset = (dgv.RowHeadersWidth - textSize.Width) / 2 ;

e.Graphics.DrawString(
    text != null ? text : "",
    dgv.Font,
    new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor),
    new PointF((float)e.RowBounds.Left + fudgeOffset + offset, (float)e.RowBounds.Top + 4));
}


-1
投票

尝试了其他答案,但我无法将相同的字体外观与列字体匹配。 我在 Microsoft 论坛

上找到了解决方案

首先订阅

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

-4
投票
 'Datagridview'.rowheadervisible=false 

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

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

 Example.rowheadervisible=false
© www.soinside.com 2019 - 2024. All rights reserved.