Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I am creating one application and I have one problem, Please help me guys.

I am creating texting textbox in gridview from code behind c#.
and number of rows and columns are depend on one dropdown so number and columns will vary. Now in gridview very cell create textbox.
I got solution for this. How to create text for this.
following is code
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               for(int i =0; i < e.Row.Cells.Count; i++)
                {
                    TextBox txt = new TextBox();
                    e.Row.Cells[i].Controls.Add(txt);
                }
            }
        }


Now i will add some values in every textbox of every cell of gridview and I want to save thease all values to database.
I tried some code as follows,

C#
protected void btnSave_Click(object sender, EventArgs e)
{
   for (var row = 0; row < GridView1.Rows.Count; row++)
    {
       for (int col = 0; col < GridView1.Rows[row].Cells.Count; col++)
         {
           TextBox txt = (TextBox)GridView1.Rows[row].Cells[col].Controls[0];
           string texboxvalue= txt.Text;

         }
    }

}

But I am not getting data from textbox and it this code giving any exception as follows
C#
"Specified argument was out of the range of valid values.
Parameter name: index"


So please help me to solve this problem.

What I have tried:

C#
protected void btnSave_Click(object sender, EventArgs e)
{
   for (var row = 0; row < GridView1.Rows.Count; row++)
    {
       for (int col = 0; col < GridView1.Rows[row].Cells.Count; col++)
         {
           TextBox txt = (TextBox)GridView1.Rows[row].Cells[col].Controls[0];
           string texboxvalue= txt.Text;

         }
    }

}
Posted
Updated 28-May-16 8:01am
Comments
F-ES Sitecore 28-May-16 11:49am    
On postback you need to re-add all of the controls you created dynamically as .net doesn't persist them for you.

http://forums.asp.net/t/1965764.aspx?Dynamically+creating+controls
Veeshal Mali 28-May-16 12:03pm    
please let me know for gridview GridView1_RowDataBound() event handler sir.
Karthik_Mahalingam 28-May-16 12:37pm    
instead of gridview, you shall go with creating dynamic Table.
or you can use hidden controls to get the values from dynamic gridview.
refer:Dynamically adding textbox and saving data in textbox in ASP.NET[^]

1 solution

Try this,
It might help you.

Create unique Id for the TextBox Control as below
and in the post back we can use Request.Form [^] object to get the values of the particular text boxes, which we can query using the name attribute of the Textbox.

C#
int i = 0;
     protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               TextBox txt = new TextBox() { ID = "txtDynamic" + i++ };
               e.Row.Cells[0].Controls.Add(txt);

           }
       }

       protected void btnSave_Click(object sender, EventArgs e)
       {

           var Column1TextBoxes = Request.Form.AllKeys.Where(k => k.Contains("txtDynamic")).ToList();
           for (int i = 0; i < Column1TextBoxes.Count; i++)
           {
               string value = Request.Form[Column1TextBoxes[i]]; // Textbox values
           }
       }


Similarly you can create multiple controls on run time and get the values by using the name of the controls.

Note: After Postback you will have to rebind the GridView with the updated values, else you wont be able to retain the data.
 
Share this answer
 

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