In FormTemplate I place combobox that select value from LinqDataSource, I want to find Selected Row’s Key value inside Linq’s selecting event at server side that will fire when user click on Add/Edit row in grid.
My code is.
<asp:LinqDataSource ID="linq_Students" runat="server"
ContextTypeName="dbLibrary.StudentDataContext"
TableName="tbl_Students" OnSelecting="linq_Student_Selecting">
</asp:LinqDataSource>
<telerik:RadGrid ID="gv_Student" runat="server"...
<MasterTableView...
<Columns>
...
</Columns>
<EditFormSettings...
<FormTemplate>
<telerik:RadComboBox ID="list_Students" runat="server" SelectedValue='<%# Bind("StudentID") %>' DataSourceID="linq_Students"
DataTextField="StudentName" DataValueField="StudentId">
</telerik:RadComboBox>
...
protected void linq_Student_Selecting (object sender, LinqDataSourceSelectEventArgs e)
{
Here I want to find Key Value.
}
With ASP Grid we can obtain the same behaviour using following technique.
<asp:LinqDataSource ID="linq_Students" runat="server"
ContextTypeName="dbLibrary.StudentDataContext"
TableName="tbl_Students" OnSelecting="linq_Student_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="ASPGridName" DefaultValue="0"
Name="StudentID" PropertyName="SelectedValue"
Type="Int32"/>
</SelectParameters>
</asp:LinqDataSource>
protected void linq_ContactPersons_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
Console.WriteLine(e.SelectParameters.ElementAt(0).Value);
}
11 Answers, 1 is accepted
You can get the data key value in the ItemDataBound event when the grid row enters edit mode as follows:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
{ |
if (e.Item is GridEditableItem && e.Item.IsInEditMode) |
{ |
GridEditableItem item = e.Item as GridEditableItem; |
int studentID = Convert.ToInt32(item.GetDataKeyValue("StudentID")); |
} |
} |
And StudentID should be added to the DataKeyNames of the MasterTableView.
I hope this helps.
Regards,
Tsvetoslav
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
We can find Key value inside any grid event but I want to find key value inside LINQ’s Selecting event.
Currently what I am doing…
I declare one page level variable, after that in Grid’s ItemCommand event I find Key value and store into page level variable, finally I access page level variable inside LINQ’s selecting event.
Instead of writing these much code it will be great if I find selected row’s key value inside LINQ’s selecting event.
As I know we can find ASP Grid’s Key value inside LINQ’s selecting event, I hope some how we should be able to find key value inside LINQ’s selecting event with RadGrid.
That's correct, however, I could not see any SelectParameters for the linq data source to which you are binding the grid. If you are using the MS GridView and you do not have any SelectParameters for the data source, how do you go about getting the data key value? Thanks in advance for the clarification.
Regards,
Tsvetoslav
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Hi,
I have created one sample example.
I put asp: GridView and Telerik:RadGrid these Grid bind using LinqDataSource I can find asp:GridView’s DataKey inside LinqDataSource’s Selecting event, I want how can find RadGrid’s DataKey value inside LinqDataSource’s selecting event?
----------------------------------------------------------------------------------------------------------------
Sample
----------------------------------------------------------------------------------------------------------------
aspx page
--------------------------
<div style="font-size: 1.2e; font-weight: bold; padding-left: 150px">
ASP Grid
</div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSourceASP" PageSize="10"
AllowPaging="true" Font-Bold="false" Width="400px" HeaderStyle-HorizontalAlign="Left"
DataKeyNames="LocationID" AutoGenerateColumns="true">
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="lnkbtn_ViewEdit" runat="server" CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<div style="font-size: 1.2e; font-weight: bold; padding-left: 150px">
Telerik Grid
</div>
<radG:RadGrid ID="RadGrid1" DataSourceID="LinqDataSourceTelerik" AllowSorting="True" runat="server"
Width="400px" AllowPaging="True" GridLines="None">
<MasterTableView AutoGenerateColumns="true" ShowFooter="true" DataSourceID="LinqDataSourceTelerik">
<Columns>
<radG:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lnkbtn_ViewEdit" runat="server" CommandName="Select" Text="Select"></asp:LinkButton>
</ItemTemplate>
</radG:GridTemplateColumn>
</Columns>
</MasterTableView>
</radG:RadGrid>
<asp:LinqDataSource ID="LinqDataSourceASP" runat="server" ContextTypeName="DataClassesDataContext"
TableName="tbl_Locations" OnSelecting="LinqDataSourceASP_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="LocationID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:LinqDataSource>
<asp:LinqDataSource ID="LinqDataSourceTelerik" runat="server" ContextTypeName="DataClassesDataContext"
TableName="tbl_Locations" OnSelecting="LinqDataSourceTelerik_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="RadGrid1" DefaultValue="0" Name="LocationID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:LinqDataSource>
--------------------------
CS page
--------------------------
protected void LinqDataSourceASP_Selecting(object sender, LinqDataSourceSelectEventArgs e){
if(GridView1.SelectedDataKey != null)
Console.WriteLine(GridView1.SelectedDataKey["LocationID"]);
}
protected void LinqDataSourceTelerik_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
//How i can find RadGrid's selected value here.
Console.WriteLine(RadGrid1.SelectedValue); //Here it display always null
}
Try using the following code statement for the purpose:
Console.WriteLine( ((GridDataItem)_pastItemsGrid.SelectedItems[0]).GetDataKeyValue(
"LocationID"
).ToString());
I hope this helps.
Best wishes,
Tsvetoslav
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
I try your suggested code inside “LinqDataSourceTelerik_Selecting” but this event not fire when I click on Select button.
I think instead of CommandName="Select" it should be something differnet for telerik grid. Will you please confirm me?
I Implement code in this way.
protected void LinqDataSourceTelerik_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
//How i can find RadGrid's selected value here.
Response.Output.Write(
((GridDataItem)RadGrid1.SelectedItems[0]).GetDataKeyValue("LocationID").ToString());
}
From my understanding of your requirement ,you want to access the edited item key field in the LinqDataSource selecting event which will be fired to populate the RadCombobox once the edit form is loaded .
C#
protected void linq_Student_Selecting (object sender, LinqDataSourceSelectEventArgs e) |
{ |
foreach (GridEditFormItem editedFormItem in RadGrid2.MasterTableView.GetItems(GridItemType.EditFormItem)) |
{ |
GridDataItem parenItem = (GridDataItem)editedFormItem.ParentItem; |
string id = parenItem.GetDataKeyValue("ID").ToString(); |
} |
} |
Hope this helps...
Thanks,
Shinu
Yes that’s true in this way we can find all grid’s id value but I want to find only selected row’s id inside LINQ’s selecting event.
protected void linq_Student_Selecting(object sender, LinqDataSourceSelectEventArgs e) |
{ |
foreach (GridEditFormItem editedFormItem in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)) |
{ |
GridDataItem parentItem = (GridDataItem)editedFormItem.ParentItem; |
if (parentItem.Selected) |
{ |
string id = parentItem.GetDataKeyValue("ID").ToString(); |
} |
} |
} |
Will You please guide me how I can get selected detail record’s id value inside hierarchical grid.