【问题标题】:How to fetch 'KeyValue' from Telerik RadGrid?如何从 Telerik RadGrid 获取“KeyValue”?
【发布时间】:2012-10-03 05:38:12
【问题描述】:
<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"));

这不起作用。有替代品吗?

【问题讨论】:

  • 为什么要在 RadGrid 上指定 DataKeyNames 属性?尝试将其删除并仅保留MasterTableView

标签: asp.net telerik grid


【解决方案1】:

在标记中定义您的数据键:

< MasterTableView datakeynames="ID" dir="RTL">< /MasterTableView>

并在后面的代码中访问:

GridDataItem item = (GridDataItem)e.Item;

var key = item.GetDataKeyValue("ID");

【讨论】:

    【解决方案2】:

    试试这个

    string keyValue = radGrid.MasterTableView.DataKeyValues[e.item.ItemIndex].ToString();
    int key = Convert.ToInt32(keyValue);
    

    【讨论】:

      【解决方案3】:

      您正在尝试访问甚至没有在 radgrid 标记中指定的数据键:

      DataKeyNames="ClientID"
      
      Int32 key = Convert.ToInt32(dataItem.GetDataKeyValue("ID"));
      

      应该是:

      Int32 key = Convert.ToInt32(dataItem.GetDataKeyValue("ClientID"));
      

      【讨论】:

      • 为什么应该是'ClientID'。它可以是 DataKeyNames="ID, Name, City, etc, etc, etc."。
      • 我们通过 mastertableview 数据键名而不是 radgrid 数据键名访问我们的数据。
      • 我看到它在标记上定义为“ClientID”。我建议更改您检索值的代码以匹配定义的键名。而已。您可以将其更改为您想要的任何内容。
      • 你是对的。但我只想说,我们也可以提供多个 DataKeyName 并在需要时访问它们。 DataKeyNames="ID,姓名,地址,国家";在这种情况下,已经声明了 4 个不同的 DataKeyName。都可以在单独的语句中使用
      • 是的,确实很有可能。
      【解决方案4】:

      请尝试以下代码 sn-p。

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

      【讨论】:

      • 如果上述代码不适合您,请告诉我您要在哪种情况下评估此数据密钥。
      • 如果您发现了问题,为什么不发布解决方案?
      • @MontyPython,您能否提供有关您的问题/问题的更多信息?
      • 嗨,我试过这个代码。但它总是在 GridDataItem item = e.Item as GridDataItem 上返回 null 值;
      猜你喜欢
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多