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

Trying to access DataRowView of GroupHeader

5 Answers 234 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael Hunt
Top achievements
Rank 1
Michael Hunt asked on 10 Apr 2014, 07:42 PM
Hi,

I am using the Q3 1114 2013 version of your AJAX for ASP .NET controls.
I am using the Rad Grid and have added a checkbox to each Group Header in the grid.  I do that in ItemDataBound...

The users can then check the checkbox and click a Save button that I've put in the CommandItemTemplate.

I have code in the ItemCommand event handler that should loop through all of the GroupHeaders, and get the state of that checkbox (along with other information I've put in the GroupHeader).

MY PROBLEM:
When my code loops through the GroupHeaders of the grid (in the ItemCommand event), the DataRowView is Nothing.
My code is as follows (which is basically the same code that works when I create the checkbox in ItemDataBound):

For Each headerItem As GridGroupHeaderItem In theRadGrid.MasterTableView.GetItems(GridItemType.GroupHeader)
  Dim groupDataRow As DataRowView = CType(headerItem.DataItem, DataRowView)
Dim chk As CheckBox = headerItem.FindControl(GetGroupHeaderParentCheckboxName(groupDataRow("ParentAppType").ToString(), groupDataRow("ObjectTypeID").ToString(), groupDataRow("ParentID").ToString()))
  If (chk.Checked) Then
  selectedItemString += String.Concat(groupDataRow("ObjectTypeID").ToString(), KEY_VALUE_DELIMETER, groupDataRow("ParentID").ToString(), SETTING_PAIR_DELIMETER)
  End If
Next

In the code above, the groupDataRow variable is Nothing.  Can you tell me what I'm doing wrong?

I have other code in this same method that loops through all of the data rows in the grid and gets information from the data rows, and it works fine.

Why can I not access the DataRowView of the GroupHeader?

Thanks,
Michael

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Apr 2014, 09:05 AM
Hi Michael Hunt,

Please try the below code snippet to access the header checkbox and loop through the items in the group.

VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = "Save" Then
        For Each item As GridGroupHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)
            Dim checkBox = TryCast(item.FindControl("HeaderCheckBoxID"), CheckBox)
            If checkBox.Checked Then
                Dim childItems As List(Of GridDataItem) = item.GetChildItems().Cast(Of GridDataItem)().ToList()
                For Each child As var In childItems
                    Dim id As String = child.GetDataKeyValue("ID").ToString()
                Next
            End If
        Next
    End If
End Sub

Thanks,
Princy
0
Michael Hunt
Top achievements
Rank 1
answered on 11 Apr 2014, 09:36 AM
Thanks for the reply Princy, but that doesn't answer my main question or solve my problem.  My question / problem is:  How do I access the DataRowView of the GroupHeader, like I do during ItemDataBound?

The groupDataRow variable, in the following line of code, comes back as Nothing:

Dim groupDataRow As DataRowView = CType(headerItem.DataItem, DataRowView)

The reason why I need to do this, is because I need to get information about the parent row that I'm grouping on.  I group on 4 fields, as follows:

<GroupByFields>
  <telerik:GridGroupByField FieldName="ParentName" />
  <telerik:GridGroupByField FieldName="ParentID" />
  <telerik:GridGroupByField FieldName="ParentAppType" />
  <telerik:GridGroupByField FieldName="ObjectTypeID" />
</GroupByFields>

If the checkbox is checked, I need to use the ParentID and ObjectTypeID in a database INSERT.

I can get those values in ItemDataBound, by running the code above, and then referring to groupDataRow("ParentID") or groupDataRow("ObjectTypeID").

How do I access the DataRowView to get information on the fields I'm using to do my grouping, from the ItemCommand event handler?

Thanks,
Michael
0
Michael Hunt
Top achievements
Rank 1
answered on 11 Apr 2014, 10:03 AM
More Information for you Princy:  
- I'm manually binding the grid myself (not using OnNeedDataSource).
- I do NOT rebind the grid before I run the code above that loops through the GridHeaders (because if I did, the checkboxes in the group header would be recreated).
- I changed my code so that all group header checkboxes would have the same name of "HeaderCheckboxID", but when the code you supplied runs, that control is NOT there.

I guess that makes sense, doesn't it?

Since I dynamically create and add the checkboxes to the group header during itemdatabound, those controls aren't part of the viewstate that get sent back with the page, are they?

If that is true, they won't exist on postback, when my itemcommand code runs, right?

If all of that is true:  Is it possible to put a checkbox in a group header, and be able to get its checked property in ItemCommand?
0
Michael Hunt
Top achievements
Rank 1
answered on 11 Apr 2014, 11:55 AM
Princy, I've found that I can use a GroupHeaderTemplate and do what I want, as follows:

<GroupHeaderTemplate>
<asp:CheckBox ID="GroupHeaderCheckboxID" runat="server" Text="" />
<asp:Label runat="server" ID="ParentNameLabel" Text='<%# Eval("ParentName") %>' />
<div>
<asp:Label runat="server" ID="ParentIDLabel" Text='<%# Eval("ParentID") %>'></asp:Label>
<asp:Label runat="server" ID="ParentAppTypeLabel" Text='<%# Eval("ParentAppType") %>'></asp:Label>
<asp:Label runat="server" ID="ObjectTypeIDLabel" Text='<%# Eval("ObjectTypeID") %>'></asp:Label>
</div>
</GroupHeaderTemplate>

From there, all of the information I need is available in the ItemCommand event.

You can consider this ticket closed, but it'd still be nice to know why I can't get the DataRowView of the GroupHeader, during ItemCommand.

Thanks Much,
Michael
0
Kostadin
Telerik team
answered on 15 Apr 2014, 09:00 AM
Hi Michael,

I am afraid that you could access the DataRowView of the GridGroupHeaderItem only at ItemDataBound event handler. Additionally in this topic is described how to customize the look of the GroupHeaderItems on this event.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Michael Hunt
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Michael Hunt
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or