How do you get the selected cell of a gridview?

Posted 14 December 2012 - 12:09 PM

I am just curious about something, if i have a gridview that has rows of buttons how do i get the id of a button that is clicked?

Like say there are 4 rows of 4 buttons each, how can i tell whether i am clicking on button 1 in row 1 instead of button 1 in row 4? I've searched online but i cant seem to find a good answer for this. (I dont have code to include because i havent made anything, i am just trying to figure this out before i get started.)

Replies To: How do you get the selected cell of a gridview?

Re: How do you get the selected cell of a gridview?

Posted 14 December 2012 - 12:29 PM

You should try doing it first based off of the GridView info found on MSDN.

If you are still having issues after you have made a good faith attempt at accomplishing what you have asked, post your code up and I'm sure someone will help you out. Simply asking "How do you do [something]?" will get you nowhere on this forum 99.99% of the time.

Re: How do you get the selected cell of a gridview?

Posted 14 December 2012 - 01:10 PM

I dont like MSDN because it is incredably hard to understand them, I find it easiest to understand when I can see an actual example, not just a bunch of lines that can be put into code. In fact most sites like MSDN are often useless to me which is why I asked here in the first place.

Re: How do you get the selected cell of a gridview?

Posted 14 December 2012 - 02:05 PM

robgw3, on 14 December 2012 - 12:10 PM, said:

I dont like MSDN because it is incredably hard to understand them, I find it easiest to understand when I can see an actual example, not just a bunch of lines that can be put into code. In fact most sites like MSDN are often useless to me which is why I asked here in the first place.

I don't know if you know this or not but if you click on any of the properties listed on MSDN in the link I provided, it shows examples of how to use them. Check out the SelectedRow property.

Re: How do you get the selected cell of a gridview?

Posted 14 December 2012 - 02:11 PM

No one ever started out liking the MSDN documents, but they are still your research tool. The nice thing is you can look at all the events and methods and explore them. The notes will start to make more sense if you keep trying.

Re: How do you get the selected cell of a gridview?

The easiest way to deal with buttons in a gridview is by setting the CommandName Property of the button and then using the GridView's OnRowCommand event.

void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
// Do something
}
if (e.CommandName=="Remove")
{
// Do something else
}
}

The above example would handle the event raised by a button with a CommandName of "Add" or another event raised by a button with a CommandName of "Remove". Some CommandNames are automatically processed by the GridView, such as "Insert", "Update" and "Delete". If you are creating a custom CommandName it is best to avoid using one of these to avoid some unforeseen side effects.

And you should pay attention to the other comments, MSDN may not be the easiest to navigate but it is still an invaluable resource