locked
Problem with Gridview RRS feed

  • Question

  • User-973030546 posted

    Hi

    I'm trying to add rows programatically to my gridview, which is not binded to any data source, but ArgumentOutOfRange Exception was thrown, what should I do?!

    here is my code

     

    protected void taskGridView_Load(object sender, EventArgs e)

    {

     GridViewRow gridViewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);           

     ((GridView)sender).Controls[0].Controls.Add(gridViewRow);

     

    }

     

    thanks

     

     

     

     

     

     

    Tuesday, July 30, 2013 1:32 PM

Answers

  • User2114377676 posted

    It seems like this expression is incorrect: ((GridView)sender).Controls[0].Controls.Add(gridViewRow); Are you sure that there is at least 1 control in ((GridView)sender).Controls?

    Check these links as well: 
    http://forums.asp.net/p/1222691/2185804.aspx
    http://www.codeproject.com/Articles/16089/NewRow-GridView 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2013 1:51 PM
  • User1143442848 posted

    Hi Flor,

    As @BorysG suggested, please try to read the given links.

    And there is a code snippet  for you to refer to:

    code in .aspx

    <asp:gridview id="GridView1" runat="server" allowpaging="true" autogeneratecolumns="false"
    pagesize="5" style="margin-top: 20px;" ondatabound="GridView1_DataBound" onpageindexchanging="GridView1_PageIndexChanging">
                  <columns>
                         <asp:templatefield headertext="Letter">
                               <itemtemplate>
                                      <%# Container.DataItem %>
                               </itemtemplate>
                         </asp:templatefield>
                  </columns>
       </asp:gridview>
    

    code in .cs

       private void GetData()
            {
                string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G" };
                GridView1.DataSource = letters;
                GridView1.DataBind();
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    this.GetData();
                }
            }
    
            protected void GridView1_DataBound(object sender, EventArgs e)
            {
                for (int i = GridView1.Rows.Count + 1; i <= GridView1.PageSize; i++)
                {
                    GridViewRow row = new GridViewRow(
                        0,
                        0,
                        DataControlRowType.DataRow,
                        (i % 2 > 0) ? DataControlRowState.Normal : DataControlRowState.Alternate);
    
                    foreach (DataControlField field in GridView1.Columns)
                    {
                        TableCell cell = new TableCell();
                        cell.Text = String.Format("Row {0} added.", i);
                        row.Cells.Add(cell);
                    }
                    GridView1.Controls[0].Controls.AddAt(i, row);
                }
            }
    
    

    Best Regards,

    Lisa Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2013 1:02 AM

All replies

  • User2114377676 posted

    It seems like this expression is incorrect: ((GridView)sender).Controls[0].Controls.Add(gridViewRow); Are you sure that there is at least 1 control in ((GridView)sender).Controls?

    Check these links as well: 
    http://forums.asp.net/p/1222691/2185804.aspx
    http://www.codeproject.com/Articles/16089/NewRow-GridView 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 30, 2013 1:51 PM
  • User1143442848 posted

    Hi Flor,

    As @BorysG suggested, please try to read the given links.

    And there is a code snippet  for you to refer to:

    code in .aspx

    <asp:gridview id="GridView1" runat="server" allowpaging="true" autogeneratecolumns="false"
    pagesize="5" style="margin-top: 20px;" ondatabound="GridView1_DataBound" onpageindexchanging="GridView1_PageIndexChanging">
                  <columns>
                         <asp:templatefield headertext="Letter">
                               <itemtemplate>
                                      <%# Container.DataItem %>
                               </itemtemplate>
                         </asp:templatefield>
                  </columns>
       </asp:gridview>
    

    code in .cs

       private void GetData()
            {
                string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G" };
                GridView1.DataSource = letters;
                GridView1.DataBind();
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    this.GetData();
                }
            }
    
            protected void GridView1_DataBound(object sender, EventArgs e)
            {
                for (int i = GridView1.Rows.Count + 1; i <= GridView1.PageSize; i++)
                {
                    GridViewRow row = new GridViewRow(
                        0,
                        0,
                        DataControlRowType.DataRow,
                        (i % 2 > 0) ? DataControlRowState.Normal : DataControlRowState.Alternate);
    
                    foreach (DataControlField field in GridView1.Columns)
                    {
                        TableCell cell = new TableCell();
                        cell.Text = String.Format("Row {0} added.", i);
                        row.Cells.Add(cell);
                    }
                    GridView1.Controls[0].Controls.AddAt(i, row);
                }
            }
    
    

    Best Regards,

    Lisa Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2013 1:02 AM