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

Add Columns dynamically to the Radgrid

2 Answers 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
reguapo
Top achievements
Rank 1
reguapo asked on 08 Aug 2012, 01:08 PM
I have a Radgrid already defined in the aspx file

<telerik:RadGrid ID="rgTest" runat="server" >
    <MasterTableView AllowPaging="False" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn DataField="test" FilterControlAltText="Filter by test"
                HeaderText="Text" UniqueName="test" SortExpression="test">
        </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="test1" FilterControlAltText="Filter by test1"
                HeaderText="Test1" UniqueName="test1" >
        </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

I created 2 columns, "test" and "test1", and the datasource of the grid is defined as above:

Protected Sub rgTest_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs) Handles rgTEst.NeedDataSource
    LoadPreview()
End Sub
 
Private Sub LoadPreview()
    Dim obj As New TestDataAccess
    Dim ds As DataSet = obj.PreviewGet() 'Return a dataset from the database
    rgTest.DataSource = ds
End Sub

but that datasource returns a lot of columns based on an sproc that create columns dynamically and I don't know how many columns will created, not even the columns names, but for sure brings these 2 ("test", "test1").
The problem is that i am getting these 2 columns repeated, and i just want to show them once, How can I do this?

PD: I note when the grid is creating the columns, the ones that I don't want to repeat, come with a 1 attached to the name, test1, test11

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Aug 2012, 03:09 AM
Hi Reguapo,

Try setting the AutoGenerateColumns property of RadGrid to 'false' as shown below.

ASPX:
<telerik:RadGrid ID="rgTest" runat="server" AutoGenerateColumns="false">

Thanks,
Shinu.
0
reguapo
Top achievements
Rank 1
answered on 09 Aug 2012, 05:47 AM
Thanks for the answer.
In that case (autogeneratecolumns="false") I will not have the other columns, the ones that are created dynamically in the sproc.

what i did to solve the problem was in the itemcreated event of the radgrid, I asked for the unique column name, i know the columns coming from the dataset put a number 1 after the name of the column for the already created in design mode, then i skip those, but i wonder if exist a better way to achieve that

here is the code to solve it, but i dont like this solution at all:
Protected Sub rgTest_ColumnCreated(sender As Object, e As GridColumnCreatedEventArgs) Handles rgTest.ColumnCreated
 
        If TypeOf (e.Column) Is GridBoundColumn Then
            Dim boundColumn As GridBoundColumn = DirectCast(e.Column, GridBoundColumn)
            Select Case boundColumn.UniqueName
                Case "Test1", "Test11"
                    boundColumn.Visible = False
            End Select
       End If
End Sub
Tags
Grid
Asked by
reguapo
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
reguapo
Top achievements
Rank 1
Share this question
or