Search
Wednesday, August 20, 2008 ..:: Articles ::.. Register  Login
  
How To highlight a row in a Datagrid
by Sai Panyam Last Updated:12 Oct 2004 View Count:1208 Rating:( 6.83/ 9 ) from 18 Vote(s)
Summary
Highlighting a row in Windows Forms Datagrid assumes significance in .NET as we no longer have the property of Marquee Style for a Datagrid. To enable highlighting we have to go about it in a non intuitive way, which I will illustrate in this article.
Artifacts can be downloaded from here.
HighlightRowInDatagrid.zip (32 kb)
Highlighting a Row in a Datagrid

Windows Forms Datagrid in .NET doesn’t expose a property called MarqueeStyle which enabled VB6.0 users to easily set the highlighting on a datagrid when the user clicks on a row. The datagrid in is in edit mode by default. So when a user clicks on a row, it highlights a cell instead of the row. Even if we set the ReadOnly property to True, only the cell is highlighted. On initial examination there seems to be no easy way of highlighting a row. But we can get the desired result by manipulating the MouseUp event of the Datagrid.

 

To Highlight a row in Datagrid

 

1. Start a new windows project in VB.NET/C#

2. Drag and Drop a datagrid onto the default Form1

3. Accept the default name DataGrid1

4. Open the Code Editor for Form1

5. Expand the node “Windows Form Designer generated code”

6. After the “’Add any initialization after the InitializeComponent () call” comment add the following code

 

 

[VB.NET Code]

 

AddHandler DataGrid1.MouseUp, AddressOf highLightRow

 

[C# Code]

 

this.dataGrid1.MouseUp +=new MouseEventHandler(highLightRow);

 

 

 

 

 

7. In the body of the form add the following code

 

[VB.NET Code]

 

Private Sub highLightRow (ByVal sender As Object, ByVal e As MouseEventArgs)

 

Dim pt = New Point(e.X, e.Y)

 

Dim grd As DataGrid = CType (sender, DataGrid)

 

Dim hit As DataGrid.HitTestInfo = grd.HitTest (pt)

If hit.Type = DataGrid.HitTestType.Cell Then

 

grd.CurrentCell = New DataGridCell (hit.Row, hit.Column)

 

grd.Select (hit.Row)

 

End If

 

End Sub

 

 

[C# Code]

 

private void highLightRow (object sender, MouseEventArgs e)

{

 

Point pt =new Point (e.X, e.Y);

 

DataGrid grd =sender as DataGrid;

 

DataGrid.HitTestInfo hitInfo=grd.HitTest (pt);

 

if (hitInfo.Type ==DataGrid.HitTestType.Cell)

 

{

 

grd.CurrentCell =new DataGridCell (hitInfo.Row, hitInfo.Column);

 

grd.Select (hitInfo.Row);

 

}

 

}

 

[VB.NET code]

 

Private Sub highLightRow (ByVal sender As Object, ByVal e AsMouseEventArgs)

 

Dim pt = New Point (e.X, e.Y)

 

Dim grd As DataGrid = CType (sender, DataGrid)

 

Dim hit As DataGrid.HitTestInfo = grd.HitTest (pt)

If hit.Type = DataGrid.HitTestType.Cell Then

 

grd.CurrentCell = New DataGridCell (hit.Row, hit.Column)

 

grd.Select (hit.Row)

 

End If

 

End Sub

 

[C# Code]

 

private void highLightRow (object sender, MouseEventArgs e){

 

Point pt =new Point (e.X, e.Y);

 

DataGrid grd =sender as DataGrid;

 

DataGrid.HitTestInfo hitInfo=grd.HitTest(pt);

 

if (hitInfo.Type ==DataGrid.HitTestType.Cell)

 

{

 

grd.CurrentCell =new DataGridCell (hitInfo.Row, hitInfo.Column);

 

grd.Select (hitInfo.Row);

 

}

 

}

 

 

Whats happening here?

First we listen to the mouse up event on the DataGrid by wiring an event handler to the mouse up event. In the event handler, we first define a new Point (using the coordinate axis relative to the grid, not the form). We need the X and Y coordinates, which are passed to us in the MouseEventArgs parameter e.

Next we get a reference to our DataGrid for which the event has been raised, by casting the sender to a DataGrid object. We then define a HitTestInfo object of the DataGrid using the HitTest method on our grid. HitTest method takes a X,Y coordinate and returns the row and column at which this point is present along with other datagrid type information. This information is returned in an object of Type HitTestInfo.

Finally we can interrogate this object to see if we indeed hit a cell and if so, set the current cell of our grid to this cell and then use the Select method on our grid to select a row.

I can understand why these types and methods are called Hit, but why the 'Test'.:-)

 

Troubleshooting
Make sure that the DataGrid's (or the TableStyle) ReadOnly attribute is set to True.
Please rate the quality of this article
Poor
Outstanding
Tell me why you rated this content this way. (optional)
 
Rating Spread

  

Copyright 2002-2005 Sai Panyam   Terms Of Use  Privacy Statement