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

NeedDataSource with RadGrid Dynamic Column Added Problem

1 Answer 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 14 Mar 2014, 10:53 AM
Here is my Code, Page_load method and RadGrid1_NeedDataSource.
The result is so weird, load in the 1 page, everything work find, when click next or 2nd page, the problem is occurs. The diagram show at Attach files.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        RadGrid1.ID = "RadGrid1"
        zzz.Controls.Add(RadGrid1)
 
        AddHandler Me.RadGrid1.NeedDataSource, New GridNeedDataSourceEventHandler(AddressOf Me.RadGrid1_NeedDataSource)
 
        RadGrid1.Visible = True
        RadGrid1.AllowPaging = True
        RadGrid1.AllowSorting = True
        RadGrid1.AllowMultiRowSelection = True
        RadGrid1.MasterTableView.AutoGenerateColumns = False
        RadGrid1.PageSize = 5
        RadGrid1.AutoGenerateColumns = False
        RadGrid1.MasterTableView.EditMode = GridEditMode.Batch
        RadGrid1.ClientSettings.Selecting.AllowRowSelect = True
        RadGrid1.EnableViewState = False
 
 
        RadAjaxManager1.EnableAJAX = True
 
    End Sub
 
 
 
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
 
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter
        adapter.SelectCommand = New SqlCommand("SELECT * FROM Product", conn)
 
        conn.Open()
        Try
            adapter.Fill(myDataTable)
        Finally
            conn.Close()
        End Try
        RadGrid1.DataSource = myDataTable
 
        Dim boundColumn As GridBoundColumn
 
        For Each col As System.Data.DataColumn In myDataTable.Columns
            boundColumn = New GridBoundColumn()
            RadGrid1.MasterTableView.Columns.Add(boundColumn)
            boundColumn.DataField = col.ColumnName
            boundColumn.HeaderText = col.ColumnName
 
 
            If col.ColumnName = "proID" Then
                'boundColumn.Visible = False
                boundColumn.HeaderText = "ProductID"
 
            ElseIf col.ColumnName = "proName" Then
                boundColumn.HeaderText = "Product Name"
 
            ElseIf col.ColumnName = "proQty" Then
                boundColumn.HeaderText = "Product Quantity"
 
            ElseIf col.ColumnName = "unitPrice" Then
                boundColumn.HeaderText = "Price"
            End If
 
        Next
    End Sub

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Mar 2014, 07:53 AM
Hi Anthony,

Please create the columns in the Page_Load event and not in NeedDataSource event. Please take a look at this article to know more about Creating a RadGrid Programmatically.

Thanks,
Princy
Tags
Grid
Asked by
Anthony
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or