Programmatically Selecting a A GridView Row in ASP.NET

I recently ran into a situation where I wanted to be able to select a row in a GridView control when a page is loaded.

The obvious solution is just to (as suggested on many forums) add the “Selected” property to the row you’re interested in, but that’s fairly suboptimal. It doesn’t trigger the OnSelectedIndexChanged or OnSelectedIndexChanging events, and I don’t think it updates other properties like the SelectedIndex on the GridView. What I really wanted to do was trigger an event that would be identical to what would happen if you selected the row using the web interface.

Googling only revealed one place that claimed to have an answer, and of course, it’s the ever-annoying ExpertSexChange. I finally figured out a way to do it on my own, so I figured I’d share. (And, perhaps, deny the aforementioned site a bit of revenue. >:-) )

The trick is to use the containing Page object’s RaisePostBackEvent() method. It sounds obvious in retrospect, but I was looking for a method on the GridView object rather than the page. (There is a RaisePostBackEvent() method on the GridView object as well…but it’s protected.) Instead, you send the event to the Page object, which the control has registered itself with, and the page informs the control. Specifically, to select a row, make the event argument “Select$rowIndex”. Shazam!

4 thoughts on “Programmatically Selecting a A GridView Row in ASP.NET

  1. Can you please post a code example of this. I tried it but I can’t get it to work.

    I tried

    this.Page.RaisePostBackEvent(MyGridView,”Select$-1)”;

    But it throws this exception:

    Invalid postback or callback argument. Event validation is enabled using in configuration or in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

    I tried changing my @Page directive and my web.config but it had no effect.

    How did you get this to work?

    Thanks!

  2. I’m not at work right now, so I can’t verify this, but I doubt you can select the “-1″th row. If you want to select the first row, for instance, you’d want to do

    RaisePostBackEvent(MyGridView, “Select$1”)

    If you’re trying to select the last row, I would think that using “-1” as a shortcut is unlikely to work; you’ll probably have to check how many rows are in the GridView and explicitly select the last one.

    Does that help?

  3. Setting the index to -1 is supposed to turn the selection off, that is, set the grid so that no rows are selected. Besides, I don’t think that is my problem based on the error I am getting. To verify I tried setting the second parameter to “Select$1” but I get the same error.

    Thanks

  4. Setting the SelectedIndex property of the GridView to -1 is supposed to turn the selection off, but that’s not exactly what we’re doing here. We’re simulating what would come back from the client if they click one of the “Select” controls for a row. I don’t believe there’s an option to create an “unselect” control, so it’s not surprising that it would only accept arguments that select a row.

    Are there at least two rows in your GridView? The rows are zero-indexed, so selecting row 1 selects the /second/ row.

    I just tried doing this both from the page class (i.e., using RaisePostBackEvent(MyGridView, “Select$0”)) and from a class derived from GridView (using RaisePostBackEvent(“Select$0”)). Both worked fine.

Leave a Reply