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

[Solved] RadGrid dynamic columns with sorting/paging etc

2 Answers 154 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chetan
Top achievements
Rank 1
Chetan asked on 29 Mar 2013, 09:45 AM
Hello,

I have added RadGrid on ASPX page and I want to add columns dynamically to this RadGrid.
I am facing problem like if page gets refreshed it creates some extra columns at the end. It should maintain same columns if i sort/change page or any postback occurs  because of asp.net controls.
Here is the code that I am using.

ZZZ.BL.ReportsBL reportBl = newZZZ.BL.ReportsBL();
       protected void Page_Init(object source, System.EventArgs e)
       {
               RadGrid1.MasterTableView.Columns.Clear();
               BuildGridColumns();
        
       }
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               drpBillingCycles.DataSource = reportBl.GetBillingCycles(37);
               drpBillingCycles.DataBind();
           }
       }
       protected void drpBillingCycles_SelectedIndexChanged(object sender, EventArgs e)
       {
           int PageCount = 0;
           System.Data.DataTable cd = new System.Data.DataTable();
           RadGrid1.VirtualItemCount = PageCount;
           RadGrid1.DataSource = reportBl.GetReportData("325012", 37, RadGrid1.CurrentPageIndex, RadGrid1.PageSize, out PageCount);
           RadGrid1.DataBind();
       }
       protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
       {
           int PageCount = 0;
            RadGrid1.VirtualItemCount = PageCount;
           RadGrid1.DataSource = reportBl.("325012", 37, RadGrid1.CurrentPageIndex, RadGrid1.PageSize, out PageCount);
           //RadGrid1.DataBind();
       }
       private void BuildGridColumns()
       {
           RadGrid1.MasterTableView.Columns.Add(GetGridBoundColumn("PhoneNumber","Phone Number"));
           RadGrid1.MasterTableView.Columns.Add(GetGridBoundColumn("UserName", "Phone User Name"));
           RadGrid1.MasterTableView.Columns.Add(GetGridBoundColumn("EmpId", "Employee Id"));
           RadGrid1.MasterTableView.Columns.Add(GetGridBoundColumn("Department", "Radio Number"));
           
       }
       private GridBoundColumn GetGridBoundColumn(string DataField,string HeaderText)
       {
           GridBoundColumn boundColumn1 = new GridBoundColumn();
           boundColumn1.DataField = DataField;
           boundColumn1.UniqueName = DataField;
           boundColumn1.SortExpression = DataField;
           boundColumn1.HeaderText = HeaderText;
           return boundColumn1;
       }

2 Answers, 1 is accepted

Sort by
0
Chetan
Top achievements
Rank 1
answered on 29 Mar 2013, 10:17 AM
Ok I got it at http://www.telerik.com/community/forums/aspnet-ajax/grid/dynamically-add-columns-to-existing-radgrid.aspx
0
Chetan
Top achievements
Rank 1
answered on 29 Mar 2013, 10:17 AM
Ok I got it at http://www.telerik.com/community/forums/aspnet-ajax/grid/dynamically-add-columns-to-existing-radgrid.aspx
Tags
Grid
Asked by
Chetan
Top achievements
Rank 1
Answers by
Chetan
Top achievements
Rank 1
Share this question
or