2012-10-12 13 views
9
<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: - di cui sopra è il markup di un Telerik RagGrid di controllo che sto usando. Ho provato ad accedere al KeyValue del GridColumn, come di consueto,Come recuperare 'KeyValue' da Telerik RadGrid?

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

Questo non funziona. C'è un sostituto?

+1

Perché si specifica la proprietà 'DataKeyNames' su RadGrid? Prova a rimuoverlo e conservalo solo per 'MasterTableView' –

risposta

17

Prova con frammento di codice di seguito.

+0

Se il codice sopra riportato non funziona per te allora per favore dimmi in quale evento vuoi valutare questo datakey. –

+0

Sta funzionando. Il problema era qualcos'altro. – MontyPython

+0

Se hai trovato il problema, perché non pubblichi la soluzione? –

3

Prova questa

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

Si sta tentando di accedere a una DataKey che non è nemmeno specificato nel markup RadGrid:

DataKeyNames="ClientID" 

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

dovrebbe essere:

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

Perché dovrebbe essere "ClientID". Può essere DataKeyNames = "ID, nome, città, ecc, ecc, ecc". – MontyPython

+0

Accediamo ai nostri dati tramite mastertableview datakeynames e non con i dati di Radgrid. – MontyPython

+0

L'ho visto definito come "ClientID" sul markup. Ho suggerito di cambiare il codice in cui è stato recuperato anche il valore per far corrispondere il nome chiave definito. Questo è tutto. Puoi cambiarlo in qualunque cosa tu voglia. –

2

definire la propria chiave di dati nel markup:

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

e accesso in codice beh ind:

GridDataItem item = (GridDataItem)e.Item; 

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