I have a child grid on a page.
In it's declaration I have added a call to the event handler "OnItemDataBound".
OnItemDataBound="rgContacts_ItemDataBound"
Back in the code behind I have this..
protected void rgContacts_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem && e.Item.OwnerTableView.DataSourceID == "dsContacts")
{
Literal ltPrimaryContact = e.Item.FindControl("ltPrimaryContact") as Literal;
}
}
If there are 2 rows in the Contacts grid, then this method gets called twice as you would expect.
Furthermore, the IF condition is true in both cases.
However if I put a break point on the line within the IF condition, and in the watch window examine the value of...
e.Item.OwnerTableView.Items[e.Item.ItemIndex]
I get this error...
'e.Item.OwnerTableView.Items[e.Item.ItemIndex]' threw an exception of type 'System.ArgumentOutOfRangeException' Telerik.Web.UI.GridDataItem {System.ArgumentOutOfRangeException}
Needless to say, if I try this...
e.Item.OwnerTableView.Items[e.Item.ItemIndex]["con_Phone"].Text
Where 'con_Phone' is one of the values from the grid, I get the same error.
I am unable to extract any of the values in the current row.
I am using the code on this Telerik page. Alas it does not work.
http://www.telerik.com/help/aspnet-ajax/grid-extract-primary-key-for-parent-item-in-hierarchy-on-update-insert.html
What am I missing here?
Thanks.
Brad
4 Answers, 1 is accepted
0

Princy
Top achievements
Rank 2
answered on 26 Aug 2011, 05:57 AM
Hello Brad,
In order to access the primary key value, you can access the ParentItem and then get the DataKeyValues as shown below.
C#:
Thanks,
Princy.
In order to access the primary key value, you can access the ParentItem and then get the DataKeyValues as shown below.
C#:
protected
void
RadGrid2_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem && e.Item.OwnerTableView.DataSourceID ==
"dsContacts"
)
{
GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
string
datakey = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex][
"con_Phone"
].ToString();
}
}
Thanks,
Princy.
0

Brad
Top achievements
Rank 1
answered on 26 Aug 2011, 06:09 AM
No go this time Princy
This line
returns NULL.
Just to recap. I have a grid called rgLocations. It has a child grid called rgContacts. It is a value from the rgContacs row I need. For the record is not the Primary Key I need. It is a 'Is Main Contact' value that is on each row of Contact info. When It finds the Main Contact it is going to set a little 'tick' flag on that row via a literal inside a telerik:GridTemplateColumn, like this.
But i digress..
Why does my OwnerTable.Items have no values?
And why does your
Finally I'll add that clicking a row on the Locations grid, updates the Contacts grid just fine. So that is setup correctly. Despite that, this is nothing to do with the parent Location grid. I just need a value from the current row of the Contacts grid.
This line
GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
returns NULL.
Just to recap. I have a grid called rgLocations. It has a child grid called rgContacts. It is a value from the rgContacs row I need. For the record is not the Primary Key I need. It is a 'Is Main Contact' value that is on each row of Contact info. When It finds the Main Contact it is going to set a little 'tick' flag on that row via a literal inside a telerik:GridTemplateColumn, like this.
<
telerik:GridTemplateColumn
UniqueName
=
"TemplateColumn"
HeaderText
=
""
>
<
ItemTemplate
>
<
asp:Literal
ID
=
"ltPrimaryContact"
runat
=
"server"
/>
</
ItemTemplate
>
<
HeaderStyle
Width
=
"20px"
/>
</
telerik:GridTemplateColumn
>
But i digress..
Why does my OwnerTable.Items have no values?
And why does your
e.Item.OwnerTableView.ParentItem;
not work? Finally I'll add that clicking a row on the Locations grid, updates the Contacts grid just fine. So that is setup correctly. Despite that, this is nothing to do with the parent Location grid. I just need a value from the current row of the Contacts grid.
0

Brad
Top achievements
Rank 1
answered on 29 Aug 2011, 12:37 AM
What I have found is that if I set a breakpoint in the rgContacts_ItemDataBound method and enter this
into the watch window, I am able to examine the values in the current row by changing the int parameter.
Of course, this pretty useless as the primary key ID is not in the displayed row of data.
None the less, this shows me that i.Item has values. Just that
or this
... do not.
Brad
e.Item.Cells[3].Text
into the watch window, I am able to examine the values in the current row by changing the int parameter.
Of course, this pretty useless as the primary key ID is not in the displayed row of data.
None the less, this shows me that i.Item has values. Just that
e.Item.OwnerTableView.Items[e.Item.ItemIndex]
or this
GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
... do not.
Brad
0

Brad
Top achievements
Rank 1
answered on 29 Aug 2011, 12:59 AM
I've been on these forums long enough to know that the solution to this sort of thing is usually found after the source code is posted.
Below are 3 things.
rgLocations (RadGrid)
egContacts (RadGrid)
Finally, dsContacts. The SqlDataSource bound to rgContacts.
Note: the SqlDataSource for rgLocations is created when the page loads in the code behind.
The code below works perfectly with the grid of contacts updating every time a row on the Locations table is clicked. The problem is in the ItemDataBound event in rgContacts.
Below are 3 things.
rgLocations (RadGrid)
egContacts (RadGrid)
Finally, dsContacts. The SqlDataSource bound to rgContacts.
Note: the SqlDataSource for rgLocations is created when the page loads in the code behind.
The code below works perfectly with the grid of contacts updating every time a row on the Locations table is clicked. The problem is in the ItemDataBound event in rgContacts.
<
telerik:RadGrid
ID
=
"rgLocations"
runat
=
"server"
PageSize
=
"10"
DataSourceID
=
"dsLocations"
OnPreRender
=
"rgLocations_PreRender"
OnItemDataBound
=
"rgLocations_ItemDataBound"
OnItemCommand
=
"rgLocations_ItemCommand"
OnDataBound
=
"rgLocations_DataBound"
GridLines
=
"None"
AllowPaging
=
"True"
>
<
HeaderContextMenu
CssClass
=
"GridContextMenu GridContextMenu_Default"
></
HeaderContextMenu
>
<
MasterTableView
AllowMultiColumnSorting
=
"false"
AutoGenerateColumns
=
"False"
DataKeyNames
=
"loc_Location_ID"
ClientDataKeyNames
=
"loc_Location_ID"
DataSourceID
=
"dsLocations"
>
<
CommandItemSettings
ExportToPdfText
=
"Export to Pdf"
></
CommandItemSettings
>
<
RowIndicatorColumn
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridTemplateColumn
UniqueName
=
"TemplateColumn"
HeaderText
=
"Row"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"numberLabel"
runat
=
"server"
Width
=
"30px"
/>
</
ItemTemplate
>
<
HeaderStyle
Width
=
"30px"
/>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"loc_Trading_Name"
FilterControlAltText
=
"Filter loc_Trading_Name column"
HeaderText
=
"Business/Location"
SortExpression
=
"loc_Trading_Name"
UniqueName
=
"loc_Trading_Name"
/>
<
telerik:GridBoundColumn
DataField
=
"loc_Phone"
FilterControlAltText
=
"Filter loc_Phone column"
HeaderText
=
"Phone"
SortExpression
=
"loc_Phone"
UniqueName
=
"loc_Phone"
/>
<
telerik:GridBoundColumn
DataField
=
"add_Suburb"
FilterControlAltText
=
"Filter add_Suburb column"
HeaderText
=
"Suburb"
ReadOnly
=
"True"
SortExpression
=
"add_Suburb"
UniqueName
=
"add_Suburb"
/>
<
telerik:GridBoundColumn
DataField
=
"add_State"
FilterControlAltText
=
"Filter add_State column"
HeaderText
=
"State"
ReadOnly
=
"True"
SortExpression
=
"add_State"
UniqueName
=
"add_State"
/>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
ClientSettings
EnablePostBackOnRowClick
=
"true"
>
<
Selecting
AllowRowSelect
=
"True"
/>
<
ClientEvents
OnRowDblClick
=
"RowDblClick"
/>
</
ClientSettings
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
<
asp:TabPanel
ID
=
"tpContacts"
runat
=
"server"
>
<
HeaderTemplate
>Contacts</
HeaderTemplate
>
<
ContentTemplate
>
<
div
class
=
"CKCDialogContainer"
>
<
div
class
=
"CKCToolbarPanel"
>
<
table
>
<
tr
>
<
td
style
=
"width:54px"
><
input
type
=
"button"
value
=
"Add"
onclick
=
"showAddContactForm()"
ID
=
"Button6"
runat
=
"server"
/></
td
>
<
td
style
=
"width:54px"
><
input
type
=
"button"
value
=
"Edit"
onclick
=
"showEditContactForm()"
ID
=
"Button7"
runat
=
"server"
/> </
td
>
</
tr
>
</
table
>
</
div
>
<
div
class
=
"CKCDialog"
>
<
table
width
=
"100%"
cellpadding
=
"0"
cellspacing
=
"0"
border
=
"0"
>
<
tr
>
<
td
>
<
telerik:RadGrid
ID
=
"rgContacts"
runat
=
"server"
PageSize
=
"10"
DataSourceID
=
"dsContacts"
OnItemDataBound
=
"rgContacts_ItemDataBound"
OnItemCommand
=
"rgContact_OnItemCommand"
GridLines
=
"None"
AllowPaging
=
"True"
>
<
HeaderContextMenu
CssClass
=
"GridContextMenu GridContextMenu_Default"
></
HeaderContextMenu
>
<
MasterTableView
AllowMultiColumnSorting
=
"false"
AutoGenerateColumns
=
"False"
DataKeyNames
=
"con_Contact_ID"
ClientDataKeyNames
=
"con_Contact_ID"
DataSourceID
=
"dsContacts"
>
<
RowIndicatorColumn
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridTemplateColumn
UniqueName
=
"TemplateColumn"
HeaderText
=
""
>
<
ItemTemplate
>
<
asp:Literal
ID
=
"ltPrimaryContact"
runat
=
"server"
/>
</
ItemTemplate
>
<
HeaderStyle
Width
=
"20px"
/>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"con_Contact"
HeaderText
=
"Contact"
UniqueName
=
"con_Contact"
/>
<
telerik:GridBoundColumn
DataField
=
"con_Position"
HeaderText
=
"Position"
UniqueName
=
"con_Position"
/>
<
telerik:GridBoundColumn
DataField
=
"con_Email"
HeaderText
=
"Email"
UniqueName
=
"con_Email"
/>
<
telerik:GridBoundColumn
DataField
=
"con_Phone"
HeaderText
=
"Phone"
UniqueName
=
"con_Phone"
/>
<
telerik:GridBoundColumn
DataField
=
"con_Fax"
HeaderText
=
"Fax"
UniqueName
=
"con_Fax"
/>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
ClientSettings
EnablePostBackOnRowClick
=
"true"
>
<
ClientEvents
OnRowClick
=
"ContactRowClick"
/>
<
Selecting
AllowRowSelect
=
"True"
/>
</
ClientSettings
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
</
td
></
tr
>
</
table
>
</
div
>
</
div
>
</
ContentTemplate
>
</
asp:TabPanel
>
<
asp:SqlDataSource
ID
=
"dsContacts"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" ProviderName="System.Data.SqlClient" SelectCommand="SELECT
con_Contact_ID,
con_Firstname + ' ' + con_Surname as con_Contact,
con_Position AS con_Position,
con_Email AS con_Email,
con_Phone AS con_Phone,
con_Fax AS con_Fax,
case when con_Is_Primary = 1 then 'true' else '' end AS con_IsPrimary
FROM Contact
WHERE con_Location_ID = @loc_Location_ID" runat="server">
<
SelectParameters
>
<
asp:ControlParameter
ControlID
=
"rgLocations"
DefaultValue
=
"0"
Name
=
"loc_Location_ID"
PropertyName
=
"SelectedValue"
Type
=
"Int32"
/>
</
SelectParameters
>
</
asp:SqlDataSource
>