How to loop through columns of mixed type

3 posts, 0 answers
  1. Stuart Watton
    Stuart Watton avatar
    18 posts
    Member since:
    Sep 2009

    Posted 29 Nov 2010 Link to this post

    Dear All

    I have a snippet of vb code that is used to loop through the columns in a radgrid and save away key data relating to a user view of the grid. The data is save to a sql-server table. The code worked fine when all the columns were of type BoundColumn, however I now have a few TemplateColumns as well.

    So, how can I look through the columns of the radgrid, identify the column type, pick up the current attributes from that column and save them to a Sql table.

    Here is the code that I currently have which works great for BoundColumns

            Dim column As Telerik.Web.UI.GridBoundColumn

            For Each column In theGrid.Columns
                If column.DataField.Length > 0 And column.OrderIndex > 0 Then
                    strSQL = "INSERT INTO wt264PageSettingsColumns (ColumnID, Text, Format, DataFormat, Visible, Value, OrderIndex, Aggregate, FooterText) SELECT '" & ColID & "', '" & RTrim(column.HeaderText) & "', '" & column.DataType.ToString & "', '" & RTrim(column.DataFormatString) & "', '" & column.Display & "', '" & RTrim(column.DataField) & "', '" & column.OrderIndex & "', CASE WHEN '" & Len(RTrim(column.FooterText)) & "' > 0 THEN 1 ELSE 0 END, '" & RTrim(column.FooterText) & "' "
                    Dim Command000e As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, myComponent.SqlConnection1)
                    Command000e.CommandTimeout = 5000
                    Command000e.ExecuteNonQuery()
                End If
            Next

    Thanks in advance

    S Watton




  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Nov 2010 Link to this post

    Hello Watton,

    The following sample code shows how to loop through the GridTemplateColumn.

    ASPX:

    <Columns>
        <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" UniqueName="FirstName">
        </telerik:GridBoundColumn>
        <telerik:GridTemplateColumn DataField="FirstName">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn DataField="EmployeeID">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
    </Columns>

    VB.NET:
    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

    Thanks,
    Princy.

  3. DevCraft Release
  4. Stuart Watton
    Stuart Watton avatar
    18 posts
    Member since:
    Sep 2009

    Posted 30 Nov 2010 Link to this post

    Princy

    Many thanks for your help. The vb code you enclosed did the trick.

    Regards

    Stuart Watton

Back to Top