getting gridview row number when a template button is selected

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi I have a gridview with one of the columns as a template column with a
button. When the user selects the button is there a way to get the row
number in the button_Click event? Another one of the columns is a template
column that has a label that I have displaying the row number.
Thanks
 
Hi,

Assign the button a commandname...
and subscribe the rowcommand event of gridview...


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")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = CustomersGridView.Rows[index];
}

BEST OF LUCK

--------------
Munna

www.munna.shatkotha.com
www.munna.shatkotha.com/blog
www.shatkotha.com
 
Hi thanks for the response. I only have one button. I added the code to the
rowcommand but the event does not fire when the button is selected, perhaps
it is because my button is a template field.
 
Hi thanks for the response.  I only have one button.  I added the code to the
rowcommand but the event does not fire when the button is selected, perhaps
it is because my button is  a template field.
--
Paul G
Software engineer.



Munna said:
Assign the button a commandname...
and subscribe the rowcommand event of gridview...
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")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
      // Retrieve the row that contains the button clicked
      // by the user from the Rows collection.
      GridViewRow row = CustomersGridView.Rows[index];
}

- Show quoted text -

You have to add: onRowCommand="GridView_RowCommand" to the GridView
asp for it to recognize the RowCommand event.

regards,

Big B
 
Back
Top