Home > .net 2.0, c#, Visual Studio 2005 > GridViews – Selecting a Single Row and Highlighting

GridViews – Selecting a Single Row and Highlighting

July 19, 2007

One of my projects had the need for a bit of customization.  We needed to specify a custom “highlight” color (not a problem using a Theme) but also only select one row into Session at a time.  I’m sure there are several ways to do this (though I couldn’t find any) and I didn’t want to develop a custom control, so I just did a bit of tweaking.

To begin, I added a ButtonField column in my GridView rather than a standard CommandButton, and gave it a command name of SelectStudent.  This will be called by the GridView’s RowCommand event. The ButtonField will pass (inside the CommandArgument) the row that it’s been clicked in.  

<asp:GridView ID=”LookupGridView” runat=”server”

AutoGenerateColumns=”False” Style=”text-align: center” AllowPaging=”True”


Width=”100%” onrowcommand=”LookupGridView_RowCommand” >


<asp:ButtonField CommandName=”SelectStudent” Text=”Select” />

<asp:BoundField DataField=”StudentId” HeaderText=”Student Id” />

<asp:BoundField DataField=”FullName” HeaderText=”Student Name” />



Why not SelectedIndexChanged/Changing?  I couldn’t (read: didn’t spend hours on it) find a good way to capture when the selected index didn’t change, but the user simply wanted to “deselect” the current row.

protected void LookupGridView_RowCommand(object sender,

GridViewCommandEventArgs e)


if (e.CommandName == “SelectStudent”)


Incident currentIncident = (Incident)Session[“Incident”];


GridViewRow row =



int selectedStudent = Convert.ToInt32(row.Cells[1].Text);


if (currentIncident.StudentId == selectedStudent)


currentIncident.StudentId = 0;

row.BackColor = System.Drawing.Color.Empty;




foreach (GridViewRow viewRow in LookupGridView.Rows)


viewRow.BackColor = System.Drawing.Color.Empty;


currentIncident.StudentId = selectedStudent;

row.BackColor = System.Drawing.Color.LightGreen;




In the code above, I’m capturing when the CommandName of SelectStudent is executed and then proceeding with the logic.  The really helpful line is:

GridViewRow row =


Unlike SelectedIndexChanging, there isn’t a SelectedRow to latch on to using RowCommand.  Since our Index is passed along in the CommandArgument, we can create a reference row and latch on to it using the Index.

The rest of the logic reads as:

  1. Check the current incident (record) from Session.
  2. If the identifier matches the identifier from the selected row, then we assume we’re UNCHECKING it (the record already has this record, but we’re clicking it again, thus toggling it off).
  1. Set the record’s ID to 0 and remove the BackColor.
  • If the identifier does not match (either is a different ID or null), then we assume we’re checking this record for the first time.
    1. Go through the GridView and make sure nothing else is highlighted (to prevent multiple rows showing up colored in, we’re only allowing one “selection”).
    2. Set the record’s ID to the selected Id.
    3. Color the back color light green (which actually looks like regular green, but whatever).

    It’s a bit verbose, but gets the job done for what we need.  There’s a huge ///TODO: at the top to remind me to keep it on the forefront and see if there’s a good better way to handle situations like these.  Ideas?


    %d bloggers like this: