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

how can i get DataKey value

5 Answers 2491 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mustafa
Top achievements
Rank 1
mustafa asked on 01 Apr 2011, 08:59 AM
my radgrid sample is;

<telerik:RadGrid ID="GridView1"    OnDataBound="GridView1_DataBound"  
                    runat="server" DataSourceID="SqlDataSourceKategori"
                    AutoGenerateColumns="False" Skin="Windows7">
                    <MasterTableView DataSourceID="SqlDataSourceKategori" DataKeyNames="ID">
                        <Columns>
                          
                            <telerik:GridTemplateColumn HeaderText="Kategori">
                                <EditItemTemplate>

but i couldnt get datakeyname value at codebehind
i tried beloow

Label16.Text = GridView1.MasterTableView.DataKeyValues[0].ToString();
but response

"System.Collections.Hashtable+ValueCollection"

please help me


5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 01 Apr 2011, 09:36 AM
Hello,


Use the "GetDataKeyValue"  method to get the keyvalue of GridDataItem.

Code:
Label16.Text = GridView1.MasterTableView.Items[0].GetDataKeyValue("ID").ToString();


Have a great day... :)

Regards,
Princy.
0
mustafa
Top achievements
Rank 1
answered on 01 Apr 2011, 09:57 AM
ok thanks ,
0
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 14 Jan 2021, 06:53 PM
I know this is an old thread, but I spend a lot of time trying to find an answer. After much trial and tribulation, I found this simple line of code that works:
 
Protected Sub RadGrid1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles RadGrid1.SelectedIndexChanged
 
        Label1.Text = gvRegion.SelectedValue.ToString
 
    End Sub
0
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 14 Jan 2021, 06:55 PM

Oops, the line should read:

Label1.text = RadGrid1.SelectedValue.ToString

(Sorry 'bout that, chief)

0
Doncho
Telerik team
answered on 19 Jan 2021, 02:58 PM

Hi Sean,

Thank you for sharing this solution!

I would like to note that this approach is suitable for scenarios with single row selection (AllowMultiRowSelection="false") and also when there is only one filed set in the DataKeyNames property of the RadGrid.

In cases with disabled RowSelection, enabled MultiRowSelection or multiple DataKeyNames, using the GetDataKeyValue method is recommended, see Accessing Raw Field Data and Key Values

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
mustafa
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
mustafa
Top achievements
Rank 1
SMc
Top achievements
Rank 2
Iron
Veteran
Iron
Doncho
Telerik team
Share this question
or