Select and Highlight an entire row in DataGridView

Select and Highlight an entire row in DataGridView
C#


int rowToBeSelected = 3; // third row
if ( DataGridView1.Rows.Count >= rowToBeSelected)
{
// Since index is zero based, you have to subtract 1
DataGridView1.Rows[rowToBeSelected - 1].Selected = true;
}


VB.NET

Dim rowToBeSelected As Integer = 3 ' third row
If DataGridView1.Rows.Count >= rowToBeSelected Then
' Since index is zero based, you have to subtract 1
DataGridView1.Rows(rowToBeSelected - 1).Selected = True
End If


Comments

Author: Kiran19 Sep 2009 Member Level: Bronze   Points : 1

Good Work !!!

Thanks,
Kiran
______________________
india-jobs-opening.blogspot.com


Author: Phagu Mahato04 Mar 2014 Member Level: Gold   Points : 6

You can also try below code snippet


void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts &= ~DataGridViewPaintParts.Focus;
if ((e.State & DataGridViewElementStates.Selected) ==
DataGridViewElementStates.Selected)
{
Rectangle rowBounds = new Rectangle(
this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
this.dataGridView1.Columns.GetColumnsWidth(
DataGridViewElementStates.Visible) -
this.dataGridView1.HorizontalScrollingOffset + 1,
e.RowBounds.Height);

using (Brush backbrush =
new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
this.dataGridView1.DefaultCellStyle.SelectionBackColor,
e.InheritedRowStyle.ForeColor,
System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(backbrush, rowBounds);
}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: