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

Dynamic Grid Creation Error

5 Answers 60 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Velkumar
Top achievements
Rank 2
Velkumar asked on 17 Nov 2011, 11:05 AM
Hi,
I am creating a Grid dynamically and i am using grid.columns.Clear() to clear the columns before adding the new columns to the grid.But that causes the following error,and i have tried to set GridTableView.EnableColumnsViewState to False but that does not work.
Please give the solution for this problem.

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ArrayList.get_Item(Int32 index) +2776221
   Telerik.WebControls.GridColumnCollection.x107df2cdc54f1163() +635
   Telerik.WebControls.GridTableView.x2f42edb67de223fa() +2342
   Telerik.WebControls.RadGrid.xecc39c6829764c81(ArrayList x968349690f5f2dca, GridTableView x97cb49e21273638b) +73
   Telerik.WebControls.RadGrid.SaveViewState() +439
   System.Web.UI.Control.SaveViewStateRecursive() +137
   System.Web.UI.Control.SaveViewStateRecursive() +254
   System.Web.UI.Control.SaveViewStateRecursive() +254
   System.Web.UI.Control.SaveViewStateRecursive() +254
   System.Web.UI.Control.SaveViewStateRecursive() +254
   System.Web.UI.Page.SaveAllState() +602
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5016

5 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2011, 11:14 AM
Hello,

if (RadGrid1.MasterTableView.Columns.Count > 0)
      {
          RadGrid1.MasterTableView.Columns.Clear();
      }




Thanks,
Jayesh Goyani
0
Velkumar
Top achievements
Rank 2
answered on 17 Nov 2011, 11:47 AM
Hi,

             I have tried this also but it does not work.
Thanks,
Velkumar.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2011, 12:00 PM
Hello,

Can you please provide your code?

let me know why you need to clear all columns? Because in dynamic control creation its not needed.

Thanks,
Jayesh Goyani
0
Velkumar
Top achievements
Rank 2
answered on 17 Nov 2011, 12:39 PM
Hi,

  I need to clear the columns because i have to add different columns in different postbacks.

Thanks,
Velkumar.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Nov 2011, 02:08 PM
Hello,

Please check below code snippet it works perfectly for me.

protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid RadGrid5 = new RadGrid();
        RadGrid5.ID = "RadGrid5";
        RadGrid5.AutoGenerateColumns = false;
 
        RadGrid5.MasterTableView.Columns.Clear();
 
        if (DateTime.Now.Second > 30)
        {
            GridBoundColumn b1 = new GridBoundColumn();
            b1.DataField = "CustomerName";
            b1.HeaderText = "CustomerName";
            RadGrid5.MasterTableView.Columns.Add(b1);
        }
 
 
        GridBoundColumn b2 = new GridBoundColumn();
        b2.DataField = "CustomerId";
        b2.HeaderText = "CustomerId";
        RadGrid5.MasterTableView.Columns.Add(b2);
 
        dynamic data = new[] {
                new { CustomerId = 1, CustomerName = 2},
                new { CustomerId = 4, CustomerName = 5}
                
            };
        RadGrid5.DataSource = data;
        RadGrid5.DataBind();
        this.Controls.Add(RadGrid5);
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Velkumar
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Velkumar
Top achievements
Rank 2
Share this question
or