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

Get selected row data (code behind)

4 Answers 1035 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lahiru
Top achievements
Rank 1
Lahiru asked on 09 Jan 2013, 01:24 PM
Hi,

I am trying to get a column value of a selected row in the radgrid.
I am using "DataKeyValues" in my C# code to retrieve the value. But it always returns an empty string. When I debug I can see the key but the value for that is empty. 
Following is my code. Please let me know if I am missing something.

Thank you,
Lahiru

ASPX page
<telerik:RadGrid ID="gridDevices" runat="server" Width="900" AllowSorting="true"   
                  AllowCustomPaging="True" AllowPaging="True" AllowFilteringByColumn="true" 
                    PageSize="10" AllowMultiRowSelection="true" OnItemCommand="RadGrid1_ItemCommand">
                <MasterTableView DataKeyNames="DeviceID"   ClientDataKeyNames="DeviceID">
            <Columns>            
                <telerik:GridBoundColumn DataField="DeviceID"  Visible="false" UniqueName="DeviceID">
                </telerik:GridBoundColumn>
             <telerik:GridTemplateColumn HeaderText="IP Address" 
                ShowFilterIcon="false" AutoPostBackOnFilter="true" UniqueName="IPAddress" DataField="IPAddress" SortExpression="IPAddress">  
                <ClientItemTemplate>
                <span title="View device web page"><a href="http://#=IPAddress#" target="_blank">#=IPAddress#</a>
</span>
                </ClientItemTemplate>
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn Visible="true" HeaderText="MAC Address" ShowFilterIcon="false" AutoPostBackOnFilter="true" UniqueName="MACAddress" DataField="MACAddress" SortExpression="MACAddress">
                <ClientItemTemplate>
                <span title="#=MACAddress#> #=MACAddress# </span>
                </ClientItemTemplate>
                </telerik:GridTemplateColumn> 
        </Columns>
        </MasterTableView>
        <PagerStyle Mode="Slider" AlwaysVisible="true" ></PagerStyle>      
       <ClientSettings>
            <DataBinding Location="SupplyGridData.asmx" SelectMethod="GetDeviceRecords">
            </DataBinding>
            <ClientEvents  OnDataBinding="DevicesGrid_DataBinding" >          
            </ClientEvents>        
            <Selecting AllowRowSelect="True"></Selecting>
        </ClientSettings>
                </telerik:RadGrid>


C# Code
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = (GridDataItem)e.Item;
                string deviceID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["DeviceID"].ToString();
            }
            
        }

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Jan 2013, 04:08 AM
Hi,

Try the following code to achieve your scenario.
C#:
protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "commandname")
    {
        foreach (GridDataItem item in RadGrid2.SelectedItems)
        {
            string value = item.GetDataKeyValue("DeviceID").ToString();
        }
    }
}

Thanks,
Shinu.
0
Lahiru
Top achievements
Rank 1
answered on 10 Jan 2013, 06:41 AM
Hi Shinu,

Thank you for your suggestion.

But still no luck. :(

I have tried most of the suggestions in these forum threads. Since non of them worked I guessed may be there is something wrong in my ASPX code. That is why I decided to post it here.

As you can see I have ClientDataKeyNames="DeviceID" and with this I am successfully able to get the correct value from javascript. The problem I am facing is on the server side. 

1. Is there any other property that I need to set in the ASPX page other than DataKeyNames="DeviceID"?
2. It shouldn't be a problem as I am using client side web service binding, right?

Thank you,
Lahiru
0
Kaushal
Top achievements
Rank 2
answered on 10 Jan 2013, 01:17 PM
Hi Lahiru,

You just Need to Add DataKeyNames="DeviceID" on MasterTableView
and past this code in ItemCommand Event will do the trick
gridDevices.MasterTableView.DataKeyValues[e.Item.ItemIndex]["DeviceID"];
0
Accepted
Shinu
Top achievements
Rank 2
answered on 11 Jan 2013, 04:35 AM
Hi,

With reference to this forum thread. When the grid is bound to Web service data provider, the only place where you can retrieve the key value of the selected row, is on the client-side. You need to set the ClientDataKeyName property correctly in order to achieve this goal.

When the grid is not bound on the server the DataKeyValue is not populated, because grid is not aware about the client binding.

Thanks,
Shinu.
Tags
Grid
Asked by
Lahiru
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Lahiru
Top achievements
Rank 1
Kaushal
Top achievements
Rank 2
Share this question
or