Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch 'KeyValue' from Telerik RadGrid?

<telerik:RadGrid ID="radGrid" runat="server" AllowPaging="true" AllowCustomPaging="True"
    GridLines="None" PageSize="20" AllowMultiRowSelection="true" ClientSettings-Selecting-AllowRowSelect="true"
    AutoGenerateColumns="false" onneeddatasource="radGrid_NeedDataSource" OnItemCreated="radGrid_ItemCreated"
    OnItemDataBound="radGrid_ItemDataBound" OnItemCommand="radGrid_ItemCommand"
    DataKeyNames="ClientID">
    <mastertableview datakeynames="ID" gridlines="None" width="100%">
    <PagerTemplate> and so on ... </telerik:RadGrid>

Scenario : - Given above is the markup of a Telerik RagGrid control I am using. I tried to access the KeyValue of the GridColumn, the usual way,

Int32 key = Convert.ToInt32((e.Item as GridDataItem).GetDataKeyValue("ID"));

This is not working. Is there an alternate?

like image 544
MontyPython Avatar asked Oct 12 '12 11:10

MontyPython


2 Answers

Please try with below code snippet.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{

    if (e.Item is GridDataItem)
    {
        // NORMAL MODE
        GridDataItem item = e.Item as GridDataItem;
        string strId = item.GetDataKeyValue("ID").ToString();
    }

    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {

        if (e.Item is GridEditFormInsertItem)
        {
            // INSERT MODE
            GridEditableItem editedItem = e.Item as GridEditableItem;

        }
        else
        {
            // EDIT MODE
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string strId = editedItem.GetDataKeyValue("ID").ToString();

        }

    }
}


protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "YourCommandName")
    {
        GridDataItem item = e.Item as GridDataItem;
        string strId = item.GetDataKeyValue("ID").ToString();
    }
}
like image 81
Jayesh Goyani Avatar answered Sep 30 '22 21:09

Jayesh Goyani


Try this

string keyValue = radGrid.MasterTableView.DataKeyValues[e.item.ItemIndex].ToString();
int key = Convert.ToInt32(keyValue);
like image 35
codingbiz Avatar answered Sep 30 '22 23:09

codingbiz