---
title: "Clear Textboxes in Gridview Cells"  
description: "Clear Textboxes in Gridview Cells"  
author: "Samuel Fernandes"  
published: 2013-11-14  
updated: 2013-11-14  
canonical: https://www.mindstick.com/forum/1729/clear-textboxes-in-gridview-cells  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Clear Textboxes in Gridview Cells

The [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) has [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) [rows and columns](https://www.mindstick.com/forum/161180/how-to-compare-rows-and-columns-in-sql-server-database) and each cell has a [textbox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview), [validator](https://www.mindstick.com/forum/156905/what-is-data-annotation-validator-attribute-in-asp-dot-net-mvc) controls. The columns are generated dynamically, and I would like to clear all the textboxes.

This is not working. Where am I going [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when)

```
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= "";                   }               }            }        }     }  }}
```

## Replies

### Reply by Pravesh Singh

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


---

Original Source: https://www.mindstick.com/forum/1729/clear-textboxes-in-gridview-cells

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
