Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView Row with a line underneath.
 

DataGridView Row with a line underneath.

Hi.

I hope someone can help with this small problem on a DataGridView.

I want to be able to put a thick black line under row 4 of my DataGridView, regardless of how many columns there are. Let's say it would be approximately 3 times wider than the standard horizontal gridline.

How can I do this in C# (my DataGridView is programmatically created)?

I also need to make sure that the grid is still visible between the cells so I can identify the columns easily.

Thanks in advance for any help given!

Sean.

Sean Connolly  Friday, February 02, 2007 4:22 PM
You can always use the RowPostPaint event to draw the line. This example draws a red line on the bottom of the third row



private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == 2)
{
int l = e.RowBounds.Left + dataGridView1.RowHeadersWidth;
int r = e.RowBounds.Right;
int b = e.RowBounds.Bottom - 2;

e.Graphics.DrawLine(Pens.Red, l, b, r, b);
}
}



Ken Tucker  Friday, February 02, 2007 8:06 PM
This should get you started:

public partial class Form1 : Form
{
private int rowIndex = 4;
private int extraHeight = 10;

public Form1()
{
InitializeComponent();
this.dataGridView1.ColumnCount = new Random().Next(100);
this.dataGridView1.RowCount = 30;

this.dataGridView1.Rows[this.rowIndex].Height += this.extraHeight;
this.dataGridView1.RowPostPaint +=new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
}

void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == this.rowIndex)
{
RectangleF originalClip = e.Graphics.ClipBounds;
Rectangle rowRectangle = this.dataGridView1.GetRowDisplayRectangle(this.rowIndex, true);
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(rowRectangle.X, rowRectangle.Y, rowRectangle.Width, this.extraHeight));
}
}
}
timvw  Friday, February 02, 2007 8:15 PM
You can always use the RowPostPaint event to draw the line. This example draws a red line on the bottom of the third row



private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == 2)
{
int l = e.RowBounds.Left + dataGridView1.RowHeadersWidth;
int r = e.RowBounds.Right;
int b = e.RowBounds.Bottom - 2;

e.Graphics.DrawLine(Pens.Red, l, b, r, b);
}
}



Ken Tucker  Friday, February 02, 2007 8:06 PM
This should get you started:

public partial class Form1 : Form
{
private int rowIndex = 4;
private int extraHeight = 10;

public Form1()
{
InitializeComponent();
this.dataGridView1.ColumnCount = new Random().Next(100);
this.dataGridView1.RowCount = 30;

this.dataGridView1.Rows[this.rowIndex].Height += this.extraHeight;
this.dataGridView1.RowPostPaint +=new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPostPaint);
}

void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
if (e.RowIndex == this.rowIndex)
{
RectangleF originalClip = e.Graphics.ClipBounds;
Rectangle rowRectangle = this.dataGridView1.GetRowDisplayRectangle(this.rowIndex, true);
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(rowRectangle.X, rowRectangle.Y, rowRectangle.Width, this.extraHeight));
}
}
}
timvw  Friday, February 02, 2007 8:15 PM

You can use google to search for other answers

Custom Search

More Threads

• asp.net + vb.net; error index out of range
• conversion of units before save or insert into database
• Exposing 2-deep properties in databinding
• Winforms Datagrid Control Sorting problem while drag-and-drop
• One record from a datatable into two rows in a datagrid view
• Edit selected record on datagridview on a new form
• DataGridView and LinkLabel
• ms access and c#
• How to add Radiobutton To Datagridview(Vb.net Windows Application)
• Problem with PositionChanged event.