Hi team
Can anyone tell me how we can add rows dynamically in gridview.
DataTable dt = new DataTable();
try
{
CheckBox chk = new CheckBox();
foreach (GridViewRow row in gvDept.Rows)
{
chk = (CheckBox)row.FindControl("ChkDept");
if (chk.Checked == true)
{
Label lbl = (Label)row.FindControl("lblcode");
string code = lbl.Text;
dt = BLSht.GetEmployeeListAsPerDept(code);
if (dt.Rows.Count>0)
{
DataRow newRow = dt.NewRow();
gvAppEmployees.DataSource = dt;
}
else
{
}
}
else
{
}
}
gvAppEmployees.DataSource = dt;
gvAppEmployees.DataBind();
}
catch (Exception ex)
{
}
}
Loading
Krishna GaradPosted Jan 4, 2011, 4:33 AM
You can add dynamic rows in datatable as like bellow. Before binding your datatable to grid you have to create the structure for table like bellow and then you have to add the rows in table then only you can bind the table to your grid.
DataTable dt = new DataTable("emp");
DataColumn dc1 = new DataColumn("Empno", typeof(int));
DataColumn dc2 = new DataColumn("Ename", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr = dt.NewRow();
dr["Empno"] = Convert.ToInt32("1");
dr["Ename"] = "Some Name";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["Empno"] = Convert.ToInt32("2");
dr1["Ename"] = "Some Another Value";
dt.Rows.Add(dr1);
Gridview1.Datasource=dt;
GridView1.DataBind();
Krishna GaradPosted Jan 4, 2011, 5:24 AM
Download the attached file it is your full solution for all the post about transfering data from one grid to another grid.