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

Rad Grid Get Control Type of Control Inside Grid Template Column

9 Answers 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tracy
Top achievements
Rank 1
Tracy asked on 20 Sep 2013, 03:42 PM
Hi,

I am dumping the contents of the rad grid into a data table and am trying to find a control that is inside the item template of a grid template column without knowing what he control name is.

I am  looping through all the columns in the grid and based on the data type, performing some custom formatting before inserting the row into the data table.

My question is, if the column is a grid template column, how can I find the control that is inside the <Item Template> without actually knowing the name.

Below is the code I am using to loop through the grid.
Dim dr As DataRow
 For Each dataItem As GridDataItem In Me.rgvMainGrid.MasterTableView.Items
     dr = exportTable.NewRow
     For Each column As GridColumn In Me.rgvMainGrid.MasterTableView.Columns
         If column.Display = True Then
             Select Case column.ColumnType
                 Case "GridBoundColumn", "GridDropDownColumn", "GridDateTimeColumn", "GridNumericColumn", "GridCalculatedColumn"
                     strFields = strFields + IIf(strFields = Nothing, "", ",") + column.UniqueName.ToString + "," + column.HeaderText.ToString
                     Select Case column.DataType.ToString
                         Case "System.String"
                             If dataItem(column.UniqueName).Text.ToString = " " Then
                                 dr(column.UniqueName) = ""
                             Else
                                 dr(column.UniqueName) = "'" + dataItem(column.UniqueName).Text.ToString
                             End If
                         Case "System.Decimal", "System.Double", "System.Single"
                             If dataItem(column.UniqueName).Text.ToString = " " Then
                                 dr(column.UniqueName) = 0
                             Else
                                 dr(column.UniqueName) = FormatNumber(dataItem(column.UniqueName).Text.ToString, 2, TriState.False, TriState.False, TriState.False)
                             End If
                         Case "System.Int16", "System.Int32", "System.Int64", "System.Single"
                             If dataItem(column.UniqueName).Text.ToString = " " Then
                                 dr(column.UniqueName) = 0
                             Else
                                 dr(column.UniqueName) = FormatNumber(dataItem(column.UniqueName).Text.ToString, 0, TriState.False, TriState.False, TriState.False)
                             End If
                         Case "System.DateTime"
                             If dataItem(column.UniqueName).Text.ToString = " " Then
                                 dr(column.UniqueName) = ""
                             Else
                                 dr(column.UniqueName) = dataItem(column.UniqueName).Text.ToString
                             End If
                     End Select
                 Case "GridTemplateColumn"
 
                 Case Else
                     'Do Nothing
             End Select
         End If
 
     Next
     exportTable.Rows.Add(dr)
 Next

Thanks for your assistance.
Tracy

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 21 Sep 2013, 05:15 AM
Hi Tracy,

Please try the following code snippet to loop through the GridTemplate Columns and access its values.

C#:
Protected Sub RadGrid1_ItemDataBound1(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        For Each col As GridColumn In RadGrid1.Columns
            If TypeOf col Is GridBoundColumn Then
                Dim v As String = item(col.UniqueName).Text
            End If
            If TypeOf col Is GridTemplateColumn Then
                ' get the value in GridTemplateColumn
                Dim QtyOrd As String = DataBinder.Eval(item.DataItem, DirectCast(col, Telerik.Web.UI.GridTemplateColumn).DataField).ToString()
            End If
        Next
    End If
End Sub

Thanks,
Princy
0
Tracy
Top achievements
Rank 1
answered on 21 Sep 2013, 03:24 PM
Hi Princy,

Thank you for your response, but I am looking to grab the information for the item template within the grid template column.
For example, if I have a template column defined like the following;
<telerik:GridTemplateColumn  DataField="ActiveFlag"      UniqueName="gtcActive"      HeaderText="Active?"        HeaderStyle-Width="85px"     SortExpression="ActiveFlag"       FilterControlWidth="65px"  FilterImageUrl="<%$ Resources:Images,FilterGray16%>"   ItemStyle-HorizontalAlign="Center" GroupByExpression="ActiveFlag Group By ActiveFlag"  >
                       <ItemTemplate>      <asp:CheckBox ID="chkActive"        runat="server"  AutoPostBack="true"  Checked='<%# Bind("ActiveFlag") %>'   OnCheckedChanged="chkActive_OnCheckChanged"  /></ItemTemplate>
                       <EditItemTemplate>  <asp:CheckBox ID="chkEditActive"    runat="server"  AutoPostBack="true"  Checked='<%# Bind("ActiveFlag") %>'/></EditItemTemplate>
                       <InsertItemTemplate><asp:CheckBox ID="chkInsertActive"  runat="server"  AutoPostBack="true"  Checked='true'   /></InsertItemTemplate>
                   </telerik:GridTemplateColumn>
 Then what I would like to do is retrieve the information for the control within the <ItemTemplate>.  In this case it would be the asp:checkbox , chkActive.  I would then like to retrieve the unique name (chkActive)  control type (checkbox)  and value("true") without actually knowing then name of the control.

Also, I need to be able to do this on a button click that is outside the grid so I need to be able to do this outside of any grid events.  

Thank You
Tracy
0
Princy
Top achievements
Rank 1
answered on 23 Sep 2013, 08:36 AM
Hi Tracy,

Please try the following code snippet to achieve your required scenario.

ASPX:
<telerik:GridTemplateColumn DataField="IsTrue" UniqueName="IsTrue" HeaderText="IsTrue?">
  <ItemTemplate>
    <asp:CheckBox ID="chkActive" runat="server" AutoPostBack="true" Checked='<%# Bind("IsTrue") %>'/>
  </ItemTemplate>                   
</telerik:GridTemplateColumn>
. . . . .
 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

C#:
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridColumn column in RadGrid1.Columns)
    {
        if (column.GetType().Name == "GridTemplateColumn")
        {
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                foreach (Control control in item["IsTrue"].Controls)
                {
                    string ColType = control.GetType().Name;
                    string Id = control.ID;
                    if (ColType.Equals("CheckBox"))
                    {
                        string value = ((CheckBox)(control)).Checked.ToString();
                    }
                }
            }
        }
    }
}

Thanks,
Princy
0
Tracy
Top achievements
Rank 1
answered on 24 Sep 2013, 01:12 AM
Thanks Princy that worked perfectly.
0
Tracy
Top achievements
Rank 1
answered on 24 Sep 2013, 01:56 AM
Hi Princy,

I spoke too soon in my previous response.
This line foreach (GridDataItem item in RadGrid1.MasterTableView.Items) loops all rows in the grid. I need to be able to pull the information for the current row only.

In my code I start with looping through all items(rows) in the grid and then for each item I loop through the columns and write the information to the data table.

See my code below.
Thanks
Tracy
'Add Rows To Export Table
Dim dr As DataRow
For Each dataItem As GridDataItem In rgvGrid.MasterTableView.Items
    dr = exportTable.NewRow
    For Each column As GridColumn In rgvGrid.MasterTableView.Columns
        If column.Display = True Then
            Select Case column.ColumnType
                Case "GridBoundColumn", "GridDropDownColumn", "GridDateTimeColumn", "GridNumericColumn", "GridCalculatedColumn"
                    '                                strFields = strFields + IIf(strFields = Nothing, "", ",") + column.UniqueName.ToString + "," + column.HeaderText.ToString
                    Select Case column.DataType.ToString
                        Case "System.String"
                            If dataItem(column.UniqueName).Text.ToString = " " Then
                                dr(column.UniqueName) = ""
                            Else
                                dr(column.UniqueName) = "'" + dataItem(column.UniqueName).Text.ToString
                            End If
                        Case "System.Decimal", "System.Double", "System.Single"
                            If dataItem(column.UniqueName).Text.ToString = " " Then
                                dr(column.UniqueName) = 0
                            Else
                                dr(column.UniqueName) = FormatNumber(dataItem(column.UniqueName).Text.ToString, 2, TriState.False, TriState.False, TriState.False)
                            End If
                        Case "System.Int16", "System.Int32", "System.Int64", "System.Single"
                            If dataItem(column.UniqueName).Text.ToString = " " Then
                                dr(column.UniqueName) = 0
                            Else
                                dr(column.UniqueName) = FormatNumber(dataItem(column.UniqueName).Text.ToString, 0, TriState.False, TriState.False, TriState.False)
                            End If
                        Case "System.DateTime"
                            If dataItem(column.UniqueName).Text.ToString = " " Then
                                dr(column.UniqueName) = ""
                            Else
                                dr(column.UniqueName) = dataItem(column.UniqueName).Text.ToString
                            End If
                    End Select
                Case "GridTemplateColumn"
                    'Find the control in the <Item Template>
                    'then get the column unique name and value
 
                Case Else
                    'Do Nothing
            End Select
        End If
 
    Next
    exportTable.Rows.Add(dr)
Next
0
Princy
Top achievements
Rank 1
answered on 24 Sep 2013, 09:41 AM
Hi Tracy,

Please try the below code snippet.

VB:
If True Then
    Dim dr As DataRow = Nothing
    For Each dataItem As GridDataItem In rgvGrid.MasterTableView.Items
        dr = exportTable.NewRow
        For Each column As GridColumn In rgvGrid.MasterTableView.Columns
            If column.Display = True Then
                Select Case column.ColumnType
                    Case "GridBoundColumn", "GridDropDownColumn", "GridDateTimeColumn", "GridNumericColumn", "GridCalculatedColumn"
                        'strFields = strFields + IIf(strFields = Nothing, "", ",") + column.UniqueName.ToString + "," + column.HeaderText.ToString
                        Select Case column.DataType.ToString
                            Case "System.String"
                                If dataItem(column.UniqueName).Text.ToString = " " Then
                                    dr(column.UniqueName) = ""
                                Else
                                    dr(column.UniqueName) = "'" + dataItem(column.UniqueName).Text.ToString
                                End If
                                Exit Select
                            Case "System.Decimal", "System.Double", "System.Single"
                                If dataItem(column.UniqueName).Text.ToString = " " Then
                                    dr(column.UniqueName) = 0
                                Else
                                    dr(column.UniqueName) = Strings.FormatNumber(dataItem(column.UniqueName).Text.ToString, 2, TriState.[False], TriState.[False], TriState.[False])
                                End If
                                Exit Select
                            Case "System.Int16", "System.Int32", "System.Int64", "System.Single"
                                If dataItem(column.UniqueName).Text.ToString = " " Then
                                    dr(column.UniqueName) = 0
                                Else
                                    dr(column.UniqueName) = Strings.FormatNumber(dataItem(column.UniqueName).Text.ToString, 0, TriState.[False], TriState.[False], TriState.[False])
                                End If
                                Exit Select
                            Case "System.DateTime"
                                If dataItem(column.UniqueName).Text.ToString = " " Then
                                    dr(column.UniqueName) = ""
                                Else
                                    dr(column.UniqueName) = dataItem(column.UniqueName).Text.ToString
                                End If
                                Exit Select
                        End Select
                        Exit Select
                    'Find the control in the <Item Template>
                    Case "GridTemplateColumn"
                        For Each control As Control In dataItem("ActiveFlag").Controls
                            Dim ColType As String = control.[GetType]().Name
                            Dim Id As String = control.ID
                            If ColType.Equals("CheckBox") Then
                                Dim value As String = DirectCast(control, CheckBox).Checked.ToString()
                            End If
                        Next
                        Exit Select
                    Case Else
                        Exit Select
                    'Do Nothing
                End Select
 
            End If
        Next
        exportTable.Rows.Add(dr)
    Next
End If

Thanks,
Princy
0
Tracy
Top achievements
Rank 1
answered on 24 Sep 2013, 02:01 PM
Hi Princy,

You code below assumes I know the column name.  I am trying to get to the control in the item template without knowing the name.  I would like to put this code in module so that I can use it on any grid.  Is there a way to reference the control without knowing the name.

Thanks
Tracy
0
Accepted
Konstantin Dikov
Telerik team
answered on 25 Sep 2013, 10:35 AM
Hello Tracy,

Could you please try to add the code snippet bellow in your "GridTemplateColumn" switch case and see if it fulfills your requirements for retrieving the value. I have tested this with several scenarios and it is getting the value without any issues:
For Each control As Control In dataItem(column.UniqueName).Controls
    Dim ColType As String = control.[GetType]().Name
    Dim Id As String = control.ID
    Dim value As String = ""
    If ColType.Equals("CheckBox") Then
        value = DirectCast(control, CheckBox).Checked.ToString()
    ElseIf control.ToString() <> "System.Web.UI.LiteralControl" Then
        value = DirectCast(control, ITextControl).Text
    End If
Next

Hope that helps.

 

Regards,
Konstantin Dikov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Tracy
Top achievements
Rank 1
answered on 26 Sep 2013, 03:47 PM
Thank you Konstantin that was what I needed.

Tracy
Tags
Grid
Asked by
Tracy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Tracy
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or