Sunday, April 25, 2010

Gridview - Highlight selected row after Postback

Problem:

I have a gridview to display employee information. When user clicks on employee no /name the particular row should be selected and selected row color should be changed. This has to be done after the postback.














Solution:


Since we are doing it after postback, the control losts the data as well selected row. We can do this with linkbutton control to change the color of the selected row very easily.


code snippet:

If you look at the snippet in the first image, I have used the linkbutton inside the itemtemplate. The important thing here is the property CommandName as Select. This property will take care of the selected row color changes. The row selection color will be taken from the SelectedRowStyle styles from the gridview.

Conclusion:


Hope this will be easy for everyone rather than doing it after the binding the grid with some extra code.

Happy coding...

No comments:

Post a Comment