I have radGrid on my web page. I now want to display a child GridTableView which will display some detail information. My radGrid is named Illustration and my GridTableView is named IllustrationDetail. I want to use the IllustrationID from my radGrid to fetch the rows from my database to populate IllustrationDetail. I do not know how to get the value of IllustrationID from my radGrid. I was trying to use "FindControl" but it gives me an error. Here is my code:
Protected
Sub rgIllustration_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles rgIllustration.DetailTableDataBind
Dim dataItem As Telerik.Web.UI.GridDataItem = CType(e.DetailTableView.ParentItem, Telerik.Web.UI.GridDataItem)
Dim IllustrationID As String = dataItem.FindControl("IllustrationID").ToString()
e.DetailTableView.DataSource = LifePolicy.GetLifePolicyIllustrationDetailList(IllustrationID)
End Sub
So how I can get the value of IllustrationID from the selected row of my RadGrid?
Thanks,
Bob
5 Answers, 1 is accepted
You can add the IllustrationID field to the DataKeyNames collection of the master table and retrieve its value using the GetDataKeyValue(fieldName) method as shown in this demo:
http://www.telerik.com/DEMOS/ASPNET/Prometheus-futures/Grid/Examples/Programming/DetailTableDataBind/DefaultVB.aspx (see the DetailTableDataBind handler)
Another option is to use declarative parent table relations.
Best regards,
Stephen
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
I changed my code and html to use the DataKeyNames collection. I have used this several times before on other pages. But I cannot get it to work here. The only difference is that now my radgrid is on a user control. The other times I used this technique it was always on web pages and worked perfectly. Is there an issue with this technique on a user control? Here is the code:
Protected
Sub rgIllustration_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rgIllustration.NeedDataSource
If Not e.IsFromDetailTable Then
rgIllustration.DataSource = LifePolicy.GetLifePolicyIllustrationList(
CInt(Request.QueryString("LifePolicyID")))
End If
End Sub
Protected Sub rgIllustration_DetailTableDataBind(ByVal source As Object, ByVal e As Telerik.Web.UI.GridDetailTableDataBindEventArgs) Handles rgIllustration.DetailTableDataBind
Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
Dim IllustrationID As String = dataItem.GetDataKeyValue("IllustrationID").ToString()
e.DetailTableView.DataSource = LifePolicy.GetLifePolicyIllustrationDetailList(
CInt(IllustrationID))
End Sub
Here is the line in error:
Dim IllustrationID As String = dataItem.GetDataKeyValue("IllustrationID").ToString()
The error is:
Object reference not set to an instance of an object.
So what am I doing wrong?
Thanks,
Bob
Can you please double-check that you added the IllustrationID field to the DataKeyNames collection of the master table? If the problem seems to be elsewhere, I suggest you paste your grid declaration along with the relevant code-behind logic in this forum post. I will examine it in detail and will get back to you with more info on the matter.
Regards,
Stephen
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Here is the HTML in my user control, Illustration.ascx, which contains the RadGrid:
<
telerik:RadGrid ID="rgIllustration"
runat="server"
AutoGenerateColumns="False"
SkinID="GridSkin"
GridLines="None"
AllowFilteringByColumn="True"
AllowPaging="true"
AllowSorting="true"
PagerStyle-Mode="NextPrevAndNumeric"
PageSize="20"
GroupingSettings-CaseSensitive="false">
<MasterTableView GridLines="both"
Name="Master"
Width="100%"
CommandItemDisplay="Top"
DataKeyNames="IllustrationID"
EditFormSettings-EditFormType="WebUserControl">
I already posted the vb.net code in my last post. And like I said in my last post, the vb.net bombs on this line:
Dim IllustrationID As String = dataItem.GetDataKeyValue("IllustrationID").ToString()
In debug mode, the value for dataItem.GetDataKeyValue("IllustrationID").ToString()
is Nothing.
I have used this same technique several times with no problems. The only difference is that now I am trying this on a user control instead of a web form.
Thanks,
Bob
This is indeed strange as your syntax seems correct and I cannot say for certain what might cause the erroneous behavior. Does using DataKeyValues instead of the GetDataKeyValue(keyField) method makes a difference?
If the problem persists, I suggest you isolate a stripped working version of your project and send it attached to a formal support ticket. I will debug the example locally and will get back to you with more info on the subject.
Best regards,
Stephen
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center