getting gridview row number when a template button is selected

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
 
M

Munna

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
 
P

Paul

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.
 
B

bwspell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top