Dynamic textbox inside gridview

Posted by Modit under ASP.NET on 8/8/2016 | Points: 10 | Views : 428 | Status : [Member] | Replies : 3
i have to create dynamic rows and columns in gridview and fill each cell with a textbox, each textbox will have a value coming from the database.

Code inside button click event

 if (rows > 0 && columns > 0)
{


string row = "";
DataTable table = new DataTable();
for (int i = 0; i < columns; i++)
{
table.Columns.Add(i.ToString(), typeof(string));

}
for (int i = 0; i < rows; i++)
{
DataRow dr = table.NewRow();

table.Rows.Add(dr);
}

GridView1.DataSource = table;
GridView1.DataBind();

}


The rows & columns are coming from textbox coming from user.

Event to place 3 textboxes in each cell and fill it up from database

int cellcount = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

for (int i = 0; i < e.Row.Cells.Count; i++)
{
string sql = "select area,uniquename,amount from propertyDescription where propertyId='" + d1.SelectedValue + "' and cellCount='" + cellcount + "' ";
DataSet ds = obj1.ExecuteStoredProcedure(sql);
Literal l1 = new Literal();
TextBox txt = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();

txt.ID = "txtArea";
txt2.ID = "txtName";
txt3.ID = "txtAmount";

txt.Width = 150;
txt2.Width = 150;
txt3.Width = 150;

txt.CssClass = "form-control";
txt2.CssClass = "form-control";
txt3.CssClass = "form-control";

txt.Text = ds.Tables[0].Rows[0][0].ToString();
txt2.Text = ds.Tables[0].Rows[0][1].ToString();
txt3.Text = ds.Tables[0].Rows[0][2].ToString();


e.Row.Cells[i].Controls.Add(txt);
e.Row.Cells[i].Controls.Add(txt2);
e.Row.Cells[i].Controls.Add(txt3);


cellcount++;
}
}
}


The above code is working fine and i am getting the records but the issue is i am not able to retrieve the values after the postback

  protected void Button1_Click1(object sender, EventArgs e)
{
Literal6.Text = "";

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];
// Literal6.Text += txt.Text+"<br>";
Literal6.Text += "" + row + "," + col + "<br>";
}
}


Can anyone help me out, thank you.




Responses

Posted by: A2H on: 8/9/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
You are adding controls to gridview dynamically. You need to add the data back on every postback. One option is to move the code to populate the gridview from RowDatabound event to RowCreated event.

You can check this link for sample code : http://forums.asp.net/post/4110436.aspx

Thanks,
A2H
My Blog

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhuvanesh6 on: 8/16/2016 [Member] Starter | Points: 25

Up
0
Down
Use Item Template,

<asp:GridView ID="GrdDynamic" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="Name" Text='<%#Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Bhuvan

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhuvanesh6 on: 8/16/2016 [Member] Starter | Points: 25

Up
0
Down
<asp:GridView ID="GrdDynamic" runat="server" OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder runat='server' ID="PlaceHolder1"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


C-Sharp Code:

public void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
//find placeholder control
PlaceHolder placeHolder = e.Row.FindControl("PlaceHolder1") as PlaceHolder;

TextBox TextBox1 = new TextBox();
placeHolder.Controls.Add(TextBox1);
}


Bhuvan

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response