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.