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

DataKeyValues

7 Answers 274 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Neelima
Top achievements
Rank 1
Neelima asked on 01 Nov 2010, 06:10 PM
Hi

How do I access DataKeyValues in a button click event handler if the button is outside the RadGrid?

<telerik:RadGrid ID="RadgridDestination" runat="server" 
     AllowSorting="True"  ShowGroupPanel="True" 
     AutoGenerateColumns="False"
          Skin="Sunset" AllowPaging="True" GridLines="None">
  
        <GroupingSettings ShowUnGroupButton="True"  />
        <ClientSettings AllowDragToGroup="True" allowcolumnsreorder="True" 
            columnsreordermethod="Reorder" reordercolumnsonclient="True" >
        </ClientSettings>
          
  
<MasterTableView EditMode="InPlace" DataKeyNames="Destination_ID" GroupLoadMode="Client" >
<CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
  
    <Columns>
<telerik:GridButtonColumn CommandName="Select" Text="Edit"  ButtonType="ImageButton"
            UniqueName="column1" 
            ImageUrl="Images/Edit.gif">
        </telerik:GridButtonColumn>
  
    </Columns>
           <EditFormSettings>
<EditColumn UniqueName="EditCommandColumn1"></EditColumn>
</EditFormSettings>
</MasterTableView>
  
  
    </telerik:RadGrid>
  
  <asp:Button ID="btnPkNO" runat="server"  CausesValidation="false" Text="NO" />
Protected Sub btnPkNO_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPkNO.Click
  
  DestID = RadgridDestination.SelectedValues("Destination_ID") 'I'm getting NullReferenceexception here
  
End Sub

Any help would be appreciated!!
Neelima


7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Nov 2010, 06:46 AM
Hello Neelima,
You can use GetDataKeyValue method to access the DataKeyValue of selected item. Sample code is given below
C#:
protected void btnPkNO_Click(object sender, EventArgs e)
   {
      GridDataItem item = (GridDataItem)RadgridDestination.SelectedItems[0];
      string i= item.GetDataKeyValue("Destination_ID").ToString();
      
   }

Thanks,
Princy.
0
Neelima
Top achievements
Rank 1
answered on 02 Nov 2010, 05:25 PM
Hi Princy
Thanks for the response.

I'm still getting that error.
0
Pavlina
Telerik team
answered on 02 Nov 2010, 06:47 PM
Hello Neelima,

Can you please verify that you are using only Advanced databinding vs Simple databinding to populate the RadGrid control?

All the best,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Neelima
Top achievements
Rank 1
answered on 03 Nov 2010, 04:29 AM
I'm using both.
Actually this RadGrid shows a list of addresses. When select button is clicked, I'm populating all the columns into a usercontrol for editing. I have an update button outside the usercontrol. when update button is clicked I get the geocode of the address to check for the validity. If the address is invalid I show a message in a modalpopup prompting if the user wants to correct it before saving. If No button (btnPkNo) is clicked I want to update the address by getting the 'Destination_ID' which is the DataKeyValue(which I'm not able to access). Once the data is updated I'm using simple databinding to bind the Radgrid.

The rest of the bindings i.e, initial databinding during the page load , to  group by a column   I'm using Advanced databinding.

Thanks
 Neelima
0
Marin
Telerik team
answered on 03 Nov 2010, 12:25 PM
Hello Neelima,

If the grid item is still selected by the time you show the confirmation popup dialog, you should be able to retrieve its DataKey without a problem. The exception that you were getting means that you do not have selected items in the grid. You can add additional check to see whether the grid has any items selected before proceeding with the rest of the code:
if (RadgridDestination.SelectedItems.Count > 0)
{
    //get selected item or selected value here
}

Also do you use server side or client-side selection? You might check this live demo showing similar functionality but with client-side selection and update. If the item that you are updating is in edit mode then you can also easily retrieve its DataKey value as shown in this help topic. The important code snipper is this:
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
editedItem.OwnerTableView.DataKeyValues(editedItem.ItemIndex)("EmployeeID")
'this gets the datakey for the item in edit mode

Let me know how this works for you and if you have any other questions.

Kind regards,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Neelima
Top achievements
Rank 1
answered on 03 Nov 2010, 05:16 PM
Marin

I'm can see the selected row highlighted after the conformation popup is shown. But in the button click event  count is 0.
RadgridDestination.SelectedItems.Count ' this count is zero.

And I'm using server-side selection. I'm using a similar structure and functionality as shown in the demo but its on server-side.
Neelima
0
Marin
Telerik team
answered on 04 Nov 2010, 11:53 AM
Hello Neelima,

I have attached a sample project similar to the server selection demo. As you can see the SelectedItems count is retrieved correctly in the button click event for the cancel button. Please verify the project on your side and let me know how to modify it in order to reproduce the problem.

Sincerely yours,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Neelima
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Neelima
Top achievements
Rank 1
Pavlina
Telerik team
Marin
Telerik team
Share this question
or