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

Code-Behind for Button - Need Row's ID Value

2 Answers 675 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 29 Sep 2015, 08:00 PM

I'm using RadGrid (it looks great!). The grid contains a few columns of data, including a non-visible column that contains each record's database ID. The last column contains a Button. It is going to be a custom Delete button --  it has to do some other things as well, so I can't just use the built-in delete button.

 My question is, how do I pass the row's ID value to my function for a button click? I'm not sure how to grab and pass that value.

2 Answers, 1 is accepted

Sort by
0
Matt
Top achievements
Rank 1
answered on 01 Oct 2015, 03:36 PM

I pieced together a solution that worked for me. Here's what I came up with.

First, I changed my button from an asp:button to a RadButton

<telerik:GridTemplateColumn UniqueName="TemplateDeleteColumn">
                        <ItemTemplate>
                            <telerik:RadButton ID="RadBtnDeleteDoc" runat="server" Text="DELETE" OnClick="RadBtnDeleteDoc_Click">
                            </telerik:RadButton>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>

And then in the code-behind, I use the GetDataKeyValue to grab the ID (drawingID) from the row that the button sits on.

protected void RadBtnDeleteDoc_Click(object sender, EventArgs e)
{
    RadButton Button = (RadButton)sender;
    GridDataItem item = (GridDataItem)Button.NamingContainer;
    int drawingID = Convert.ToInt32(item.GetDataKeyValue("DrawingID"));
 
    //now process things using drawingID....
}

I've seen a couple different ways to accomplish my goal, but since the grid already knows that "DrawingID" is a "DataKeyName", it is easy to grab it using this code.  Hope this helps out somebody else.

0
Viktor Tachev
Telerik team
answered on 02 Oct 2015, 11:19 AM
Hello Matt,

I am glad that you have the issue resolved. That is one approach you can use to retrieve the Id from the current row. Note that in order for the GetDataKeyValue method to work the DataField should be added to the DataKeyNames collection of the GridTableView.

Alternatively you can use the following approach for getting the value.

protected void RadBtnDeleteDoc_Click(object sender, EventArgs e)
{
    RadButton Button = (RadButton)sender;
    GridDataItem item = (GridDataItem)Button.NamingContainer;
    int drawingID = Convert.ToInt32(item["DrawingID"].Text);
 
 
}

If you would like additional information on accessing the values in the cells of RadGrid you would find the following article interesting.



Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Matt
Top achievements
Rank 1
Answers by
Matt
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or