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

How to loop through columns of mixed type

2 Answers 297 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stuart Watton
Top achievements
Rank 2
Stuart Watton asked on 29 Nov 2010, 08:59 PM
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 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 30 Nov 2010, 05:45 AM
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.
0
Stuart Watton
Top achievements
Rank 2
answered on 30 Nov 2010, 09:43 PM
Princy

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

Regards

Stuart Watton
Tags
Grid
Asked by
Stuart Watton
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Stuart Watton
Top achievements
Rank 2
Share this question
or