Dynamic Grid Creation Error

6 posts, 0 answers
  1. Velkumar
    Velkumar avatar
    85 posts
    Member since:
    Apr 2011

    Posted 17 Nov 2011 Link to this post

    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
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 17 Nov 2011 Link to this post

    Hello,

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




    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Velkumar
    Velkumar avatar
    85 posts
    Member since:
    Apr 2011

    Posted 17 Nov 2011 Link to this post

    Hi,

                 I have tried this also but it does not work.
    Thanks,
    Velkumar.
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 17 Nov 2011 Link to this post

    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
  6. Velkumar
    Velkumar avatar
    85 posts
    Member since:
    Apr 2011

    Posted 17 Nov 2011 Link to this post

    Hi,

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

    Thanks,
    Velkumar.
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 17 Nov 2011 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017