Hi
I have below code . Textbox is added at the end. Now i want to read its value in all Gridview rows
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();
txt.Text = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = "";
e.Row.Cells[i].Controls.Add(txt);
}
}
}Thanks

Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted May 5, 2023, 8:43 AM EDT
To read the value of the dynamically created TextBox in each row of the GridView, you can modify your code as follows:
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(); txt.ID = "txtBox" + i.ToString(); // Set a unique ID for the TextBox control txt.Text = e.Row.Cells[i].Text; e.Row.Cells[i].Text = ""; e.Row.Cells[i].Controls.Add(txt); } } }Then, to read the value of the TextBox in each row, you can handle the GridView's RowCommand or RowUpdating event. Here's an example of how you can do this in the RowCommand event:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "UpdateRow") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[rowIndex]; for (int i = 0; i < row.Cells.Count; i++) { TextBox txt = (TextBox)row.Cells[i].FindControl("txtBox" + i.ToString()); string value = txt.Text; // Do something with the TextBox value here } } }In this example, we assume that you have a button in each row of the GridView with the CommandName "UpdateRow" and the CommandArgument set to the row index. When the button is clicked, the RowCommand event is fired, and the value of each TextBox in the clicked row can be read and processed as needed.
Ramco RamcoRank #462 · 5.4K reputationPosted May 5, 2023, 9:12 AM EDT
Hi Brahma
On click of button i am looping thru all records. I have no Edit/Cancel/Update option
Secondly in Page Source it is showing like this - id="grdPlanning_txtSessionDateSystem.Web.UI.WebControls.GridViewRow_108"
Thanks