Reputation: 12517
I have a DataGrid whose data source is a collection of objects. Now the datagrid is set up to display only a subset of the objects properties. One of the hidden properties is an ID variable which I need to obtain once the row corresponding to that object has been selected.
I know you can listen for row clicks using the SelectIndexChanged event handler....but how is it possible to get at the actual original object correspoding to a row?
Upvotes: 2
Views: 693
Reputation: 1160
When using the Telerik RadGrid you should use the Telerik API's which provide you with 2 properties you can set: DataKeyNames and ClientDataKeyNames. So long as the column appears in your initial data source, whatever you specify in these 2 properties is accessible both serverside and client side for any row. Included below how to fetch those values on SelectedIndexChanged (Server-Side) and OnRowSelected (client-side)
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">
<MasterTableView DataKeyNames="Id,HiddenVal1,HiddenVal2" ClientDataKeyNames="Id,HiddenVal1,HiddenVal2,clientsideSpecialId">
<Columns>
<telerik:GridButtonColumn CommandName="Select" Text="Select" UniqueName="SelectColumn" />
<telerik:GridBoundColumn UniqueName="ContactName" HeaderText="Contact name" DataField="ContactName" />
<telerik:GridBoundColumn UniqueName="ContactTitle" HeaderText="Contact title" DataField="ContactTitle" />
</Columns>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowSelected="RowSelected"/>
</ClientSettings>
</telerik:RadGrid>
Retrieving the hidden field value server side on selected index changed:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadGrid1.SelectedItems == null || RadGrid1.SelectedItems.Count == 0)
return;
var dataItem = RadGrid1.SelectedItems[0] as GridDataItem;
if (dataItem != null)
{
var myId = dataItem.GetDataKeyValue("id").ToString();
var hiddenfield1 = dataItem.GetDataKeyValue("HiddenVal1").ToString();
//do stuff
}
}
and then retrieving the hidden field values client side on row select using ClientEvents OnRowSelected
function RowSelected(sender,eventArgs)
{
var HiddenVal1 = eventArgs.getDataKeyValue("HiddenVal1")
//do stuff
}
Upvotes: 0
Reputation: 13665
I see you tagged your question with "Telerik".
Are you using a DataGrid or a RadGrid? Yes.
Then specify which event to call in your .aspx file:
<telerik:RadGrid ID="RadGrid1" runat="server"
AutoGenerateColumns="False"
GridLines="None"
OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged" >
(...)
</telerik:RadGrid>
And declare the event in your .asp file:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
var dataItem = e.Item.DataItem;
if (dataItem != null)
{
int dataItemID = (dataItem As ObjectClassName).ID;
}
}
*Replace the "ObjectClassName" with the type of your object.
Upvotes: 1
Reputation: 6793
You can access the DataItem
as and then cast the item to the appropriate type and access its properties from there
Quick link shows how this can be accomplished.
Upvotes: 0