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

How do I identify a record using GridCommandEventArgs?

3 Answers 750 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard Byrne
Top achievements
Rank 1
Richard Byrne asked on 25 Feb 2010, 01:57 PM
Hi There,
I'm just starting to use the RadGrid and I can't see how to access the data from GridCommandEventArgs.

I have a simple grid bound to an ObjectDataSource that is a .NET generated dataset (a .xsd file in the App_Code folder).
I have set up the grid so there is a delete button on each line.

Because the dataset uses more than 1 table and the Select uses a Join I cannot use the Delete/Update commands from the dataset ( as Visual Studio can't generate the SQL for Joined tables).

So I plan to capture the Delete command and do my changes to the dataset in that method and then update back to the database.

However, I do not know how to reference the record (row) on which Delete was pressed.  I need this to get the unique ID for the record.  Once I have this reference I can use it to find the records other fields and edit or otherwise manipulate the data.
I thought this might return the first column in the record but apparantly not.

    protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
    {
        string ID = (string)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["RoleName"];  <-------This does not work

        string ID2 = (string)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][0];  <-------This does not work
    }

I have no DataKeyValues set in the aspx file because ( I believe) I generate the field names from the dataset

How do I reference the row?
Is this the right way to deal with this problem?

Thanks for any help
Richard










3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Feb 2010, 05:34 AM
Hi Richard,

You can access the row by using e.Item in DeleteCommand event. Then by using the ColumnUniqueName access the required cell value as shown below.

C#:
 
    protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        string str = item["CompanyName"].Text; 
        Response.Write(str); 
    } 

Regards,
Shinu.
0
ASHISH
Top achievements
Rank 1
answered on 03 Jun 2014, 11:40 AM
what i have put in .aspx page for  "RadGrid1_DeleteCommand" to call this one.
0
Shinu
Top achievements
Rank 2
answered on 03 Jun 2014, 12:45 PM
Hi Ashish,

I guess you want to fire the OnDeleteCommand event for the RadGrid. This event fires when we call a DeleteCommand. You can have delete on your grid using AutoGenerateDeleteColumn or having CommandName as Delete in GridButtonColumn.
Check this article for more details Deleting Records

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateDeleteColumn="true" . . >
  
   OR
 
<telerik:GridButtonColumn CommandName="Delete" Text="Delete"></telerik:GridButtonColumn>

Thanks,
Shinu
Tags
Grid
Asked by
Richard Byrne
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
ASHISH
Top achievements
Rank 1
Share this question
or