Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET DataGridView: Remove "current row" black triangle

In the DataGridView, even if you set the grid as readonly there is a black triangle at the rows headers which is shown at the current row.

I'd like to avoid it to be shown, also I'd like to avoid the big padding of those cells caused by the triangle. I guess the padding is caused by the triangle because the cell's padding is 0.

Is it posible to do that? How?

Thanks!

Edit

This is how row headers text is created:

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

and headers its a simply array of strings. (string[])

like image 865
Diego Avatar asked Apr 28 '11 21:04

Diego


3 Answers

  • If you want to keep the row headers rather than hide them, then you can use the cell padding to push the triangle out of sight:

    this.dataGridView1.RowHeadersDefaultCellStyle.Padding = 
        new Padding(this.dataGridView1.RowHeadersWidth);
    
  • If you are using row header text and want keep that visible you need to use some custom painting - thankfully very simple. After the above code, simply attach to the RowPostPaint event as shown below:

    dataGridView1.RowPostPaint += 
        new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
    

    And in the RowPostPaint method:

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

    As Dan Neely points out the use of Brushes.Black above will overwrite any existing changes, so it is better for the brush to use:

    new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
    
like image 141
David Hall Avatar answered Sep 30 '22 21:09

David Hall


Set RowHeadersVisible to false.

like image 24
Joe White Avatar answered Sep 30 '22 21:09

Joe White


A very simple solution is to set the row height to 16 pixels or less. This disables all icons in the row header cell.

dataGridView1.RowTemplate.Height = 16;
like image 29
Wizou Avatar answered Sep 30 '22 22:09

Wizou