Click here to Skip to main content
15,891,908 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I Have a problem with GridView.I am Creating seating layout using gridview.in that I am binding textboxes in each cell dynamically.Every thing is ok for first time i.e when i am clicking button the gridview is binding with required layout.But problem is when the page is post back the rowdatabound event is not firing.Can any one help me For this issue.
Thanks in advance
My code is as follows

//for binding Grid
 protected void getdata()
    {
        try
        {
            DataTable dt = new DataTable();

            for (int i = 0; i < int.Parse(txtcolumns.Text); i++)
            {
                DataColumn dcol = new DataColumn();
                //dcol.AutoIncrement = true;
                dt.Columns.Add(dcol);

            }
            for (int i = 0; i < int.Parse(txtrows.Text); i++)
            {
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lblerror.Visible = true;
            lblerror.Text = ex.Message;
        }
    }

-----------------
//Rowdatabound
 try
        {
            int inte = e.Row.RowIndex + 1;
            if (cbxseats.Checked == true)
            {
                if (inte == int.Parse(txtdivider.Text))
                {

                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        for (int i = 0; i < int.Parse(txtcolumns.Text); i++)
                        {
                            TextBox tbx = new TextBox();
                            tbx.Width = 40;
                            tbx.Height = 20;
                            tbx.ID = "txtbox" + inte + i;
                            //tbx.Text = "45";
                            //tbx.ReadOnly = true;
                            if (i + 1 == int.Parse(txtcolumns.Text))
                            {
                                e.Row.Cells[i].Controls.Add(tbx);
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < int.Parse(txtcolumns.Text); i++)
                    {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            TextBox tbx = new TextBox();
                            tbx.Width = 40;
                            tbx.Height = 20;
                            tbx.ID = "txtbox" + inte + i;
                            e.Row.Cells[i].Controls.Add(tbx);
                        }
                    }
                }
            }
            else if (cbxbearths.Checked == true)
            {
                
                if (inte == int.Parse(txtdivider.Text))
                {

                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Cells[0].Controls.Add(new LiteralControl("<br/>"));
                       
                    }
                }
                else
                {
                    for (int i = 0; i < int.Parse(txtcolumns.Text); i++)
                    {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            TextBox tbx = new TextBox();
                            tbx.Width = 80;
                            tbx.Height = 20;
                            tbx.ID = "txtbox" + inte + i;
                            e.Row.Cells[i].Controls.Add(tbx);
                        }
                    }
                }

            }
catch (Exception ex)
      {
          lblerror.Visible = true;
          lblerror.Text = ex.Message;
      }
  }

------------
//on button click event i am calling that getdata() to bind the gridview
protected void btnlayout_Click(object sender, EventArgs e)
   {
       try
       {
               getdata();
               lbllower.Visible = true;
               //btnupperdeck_Click(sender, e);
       }
       catch (Exception ex)
       {
           lblerror.Visible = true;
           lblerror.Text = ex.Message;
       }
   }
Posted
Updated 3-Mar-11 20:06pm
v5
Comments
Prerak Patel 4-Mar-11 1:43am    
share some code
Sreejith Gopinathan 4-Mar-11 1:44am    
Please verify that you have GridView data binding code in button click
karthikeya.A 4-Mar-11 1:48am    
just now i posted my code just check once
Sreejith Gopinathan 4-Mar-11 1:46am    
It will increase the chances to get proper answer if you modify your question with relevant code.
karthikeya.A 4-Mar-11 1:50am    
main issue is on button click grid is binding with text boxes but on post back the rowdata event is not firing so grid id displaying only empty rows

1 solution

DataTable dt = new DataTable();

You are generating new table everytime in getData, then how can you expect it to be filled with data.
It will always be blank table, and you'll come up with empty rows only.
 
Share this answer
 
Comments
karthikeya.A 4-Mar-11 2:43am    
Thanks for your solution but my doubt is if datatable is getting generated every time the grid view is also going to bind then why the rowdatabind event not happening next time.I am new to .net so help me please.
Prerak Patel 4-Mar-11 3:41am    
Well, then consider AlbinAbel's comment and update your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900