CLEAR TEXTBOXES IN GRIDVIEW CELLS

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
C# 
C#
 852  View(s)
Ratings:
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= "";

                   }

               }

            }

        }

     }

  }

}

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Clear Textboxes in Gridview Cells

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!