Home > DeveloperSection > Forums > Clear Textboxes in Gridview Cells
Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    November-14-2013 1:00 AM

 C# C# 
Ratings:


 1 Reply(s)
 652  View(s)
Rate this:

The gridview has multiple rows and columns and each cell has a textbox, validator controls. The columns are generated dynamically, and I would like to clear all the textboxes.

This is not working. Where am I going wrong

protected void btnClear_Click(object sender, EventArgs e)

{

 if(gvMain.Rows.Count > 0)

  {

    foreach(GridViewRow gvr in gvMain.Rows)

     {

       foreach(TableCell tc in gvr.Cells)

        {

           if(tc.HasControls())

            {

              for(int i=0;i<tc.Controls.Count;i++)

               {

                 if(tc.Controls[i] is TextBox)

                   {

                     TextBox tb = (TextBox)tc.Controls[i];

                      tb.Text= "";

                   }

               }

            }

        }

     }

  }

}



Pravesh Singh

Total Post:411

Points:2881
Posted on    November-14-2013 1:17 AM

Here is the solution

     foreach (GridViewRow row in GridView1.Rows)

        {

            foreach (TableCell cell in row.Cells)

            {

                foreach (var control in cell.Controls)

                {

                    var box = control as TextBox;

                    if (box != null )

                    {

                        box.Text = string.Empty;

                    }

                }

            }

        }

Hope this will help


Don't want to miss updates? Please click the below button!

Follow MindStick