This is a migrated thread and some comments may be shown as answers.

[Solved] Column button to change another columns value but cannot select the column

3 Answers 112 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 15 Jun 2009, 01:52 PM
Hi,

I have a grid with databound columns, these columns are readonly.  I then add a Button column, upon clicking the button, I want one of the data bound columns' value to update.  I am guessing I could more easily implement this by having a Select button and doing it that way but for other reasons, the button cannot select the row.  I am using this code in the ItemCreated event handler

        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            Button btnProcess = (Button)item["btnProcess"].Controls[0]; 
 
            if (btnProcess != null) 
                btnProcess.Click +=new EventHandler(btnProcess_Click); 
        } 

The event is fired and handled, but in the button click handler, how do I get the row details for the specific row for which the button was clicked?  Ie, if I've got 12 rows, and the button was clicked in row 4, how do I know it was the 4th row as it will not be selected?

Thanks,

3 Answers, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 15 Jun 2009, 08:10 PM
I didn't think my explanation was very good when i re-read the post.   This example is not what I want to do but might explain it better.

I have a bound grid then I add two button columns, one plus, the other minus.  Clicking either will increase or decrease an integer bound column.  Basically in the button click event handler how do I know which row raised the event?

Thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 16 Jun 2009, 04:53 AM
Hello Paul,

You can try out the following code in the button click handler, to find out which row the button belongs to:
c#:
   protected void btnProcess_Click(object sender, EventArgs e) 
    { 
        Button btn = (Button)sender; 
        GridDataItem dataItem = (GridDataItem)btn.NamingContainer; //to access the row to which the button belongs 
        string strtxt = dataItem["CompanyName"].Text; 
    } 

Thanks
Princy.
0
Paul
Top achievements
Rank 1
answered on 16 Jun 2009, 09:24 AM
Superstar.  Never seen NamingContainer before, thanks
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or