asp.net - 如何从 Telerik RadGrid 获取 'KeyValue'?

标签 asp.net telerik grid

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

场景: - 上面给出的是我正在使用的 Telerik RagGrid 控件的标记。我尝试以通常的方式访问 GridColumn 的 KeyValue,

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

这不起作用。有替代方案吗?

最佳答案

请尝试使用以下代码片段。

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();
    }
}

关于asp.net - 如何从 Telerik RadGrid 获取 'KeyValue'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12858487/

相关文章:

css - 重新对齐面板 CSS

asp.net - 使用 MSBUILD 和/或 MSDEPLOY (.NET 3.5) 从命令行进行部署

javascript - 在窗口中获取第一个可见的 iFrame 并打印它

class - 未捕获的类型错误 : Cannot call method 'insert' of undefined

c# - LINQ 组按星期几抛出异常

c# - 来自 response.write() 的 ASP.net C# 文本未按我的意愿显示

asp.net - 如何在不刷新父窗口的情况下关闭radwindow

c# - Telerik 中报告参数 XXX 的值无效

javascript - 获取选中的行列值extjs网格

java - 使用 Vaadin 7.8.4 (Java) 将数据添加到特定的列和行