Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
.NET DataGridView:Remove";“当前行”;黑三角_.net_Winforms_Datagridview - Fatal编程技术网

.NET DataGridView:Remove";“当前行”;黑三角

.NET DataGridView:Remove";“当前行”;黑三角,.net,winforms,datagridview,.net,Winforms,Datagridview,在DataGridView中,即使将网格设置为只读,在当前行中显示的行标题处也会有一个黑色三角形 我希望避免它被显示出来,同时我也希望避免三角形造成的那些单元格的大填充。我猜填充是由三角形引起的,因为单元格的填充是0 有可能这样做吗?怎么做 谢谢 编辑 这是如何创建行标题文本的: for (int i = 0; i < 5; i++) { DataGridViewRow row = new DataGridViewRow(); row.HeaderCell.Value =

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

我希望避免它被显示出来,同时我也希望避免三角形造成的那些单元格的大填充。我猜填充是由三角形引起的,因为单元格的填充是0

有可能这样做吗?怎么做

谢谢

编辑

这是如何创建行标题文本的:

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

headers
只是一个字符串数组。(
string[]

RowHeadersVisible
设置为
false

  • 如果要保留行标题而不是隐藏它们,则可以使用单元格填充将三角形推到看不见的地方:

    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指出的那样,使用
    画笔。上面的黑色将覆盖任何现有更改,因此画笔最好使用:

    new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
    

三角形问题,很简单,放

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

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

以防有人仍想知道:

dataGridView1.RowHeadersWidth = 4; // the left row header size.
这将去掉三角形并缩小默认大小

希望有帮助

dataGridView1.CurrentCell = null;
将完全删除黑色箭头


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

DataGridViewRowPostPaintEventArgs包含以下特定的PaintHeader方法:

PaintHeader(DataGridViewPaintParts) - Paints the specified parts of the row header of the current row.
这是DataGridViewPaintParts枚举:

因此,您要做的是在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

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

dataGridView1.RowTemplate.Height = 16;
隐藏黑色箭头行选择器的步骤

Datagridview是datagrid的名称,不带引号“”,因此。。。。如果您的网格名为Example,那么

 Example.rowheadervisible=false

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

首先订阅
DataGridView.cellpaining
like

dgv.CellPainting+=dgv_CellPainting

然后根据需要打印文本(原始帖子有行索引)


这对三角形很有用,但它也会移动标题文本,我想要文本而不是三角形,可以吗?@Diego嗨,不确定行标题中的文本是什么意思。像乔一样,我从未见过这样的事。请您用创建此文本的代码编辑您的问题,我来看看。@Diego哇,这是为了向您展示DataGridView中有多少选项-我已经使用了多年,从未见过HeaderCell.Value used。我在回答中添加了一些保留文本的代码。@David Hall您应该使用
新的SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
来维护任何样式更改,而不是默认为黑色。后者也会在主题更改了WindowsText颜色的任何系统上造成问题。由于它出现两个字符串,一个挨着另一个,我添加了
e.Graphics.FillRectangle(新的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)
before
e.Graphics.DrawString(…)
@Jow-White:我希望能够看到标题和标题文本,我只是不想看到令人讨厌的三角形。很抱歉,您的名字打错了。我从来没有看到行标题中的文本——只有三角形。您是否将行标题(在每行的左侧,带有灰色框和三角形)与列标题(每列顶部的名称)混淆?不,我有一个双输入表,所以我的行标题中有文本。我不确定复式输入表在英语中是否是正确的表达方式。这对我很有用。我喜欢把行号放在行标题上。字形将数字向右推,因此我必须使行标题比我想要的宽。这是一个错误。Microsoft文档说明,如果没有足够的空间容纳这两个标志符号,则headercell值将使标志符号离开屏幕。实际发生的情况正好相反:图示符将headercell.value推开。禁用showediticon也不起作用。另外,您必须禁止调整行大小。否则,用户将调整行的大小,问题又出现了。为每个图形创建新字体是一个巨大的不必要的开销。谢谢,多选随机行的问题已经消失
 Example.rowheadervisible=false
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;
            }
        }