Sep 1, 2007

GridView RowCommand event

In the RowDeleting, SelectedIndexChanging, RowUpdating, RowEditing, you can retrieve row index like e.RowIndex, or e.NewSelectedIndex, e.NewEditIndex. But GridVeiwCommandEventArgs has not index attribute. So how can you get the row index in RowCommand event? It turns out that GridViewComandEventArgs has an additional e.CommandArgument, in the events above it will return the row index. Here is senario, I want to have a button, to show related info, I can want to have command name as showInfo. But in the RowCommand event, I can not know the row index.? My solution is don't use command name as "showInfo", use "Select", this will trigger the SelectedIndexChanging event, in the event you can have e.NewSelectedIndex. But you should also you have two action button, one is showUserInfo, another is showOtherInfo, if both use select command, how to differenciate them? I will do like string commandArgs; protected void gv_RowCommand(object sender, GridViewCommandEventArgs e) { commandName = e.CommandArgument; } protected void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { int rowIndex = e.NewSelectedIndex; if (commandArgs == "showUserInfo") { //.. } else if (commandArgs == "showOtherInfo") { //.. } } This works because RowCommand happen before Select changing event.