Dynamically we can rows to gridview in asp.net please follow below code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column Name", typeof(DataType)));
dt.Columns.Add(new DataColumn(" Column Name ", typeof( DataType )));
dt.Columns.Add(new DataColumn(" Column Name ", typeof( DataType )));
dt.Columns.Add(new DataColumn(" Column Name ", typeof( DataType )));
dr = dt.NewRow();
GridBind();
}
}
private void GridBind()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Connection string");
SqlDataAdapter dap = new SqlDataAdapter("Store Procedure", con);
dap.SelectCommand.CommandType = CommandType.StoredProcedure;
dap.Fill(ds);
grd.DataSource = ds;
grd.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
DataTable dt2 = new DataTable();
SqlConnection con = new SqlConnection("Connectionstring");
SqlCommand cmd = new SqlCommand("spName", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
adp.Fill(ds);
dt2 = ds.Tables[0];
for (int i = 0; i <= dtCurrentTable.Rows.Count-1; i++)
{
//extract the TextBox values
TextBox box0 = (TextBox)grd.Rows[rowIndex].Cells[0].FindControl("ControlName");
TextBox box1 = (TextBox) grd.Rows[rowIndex].Cells[1].FindControl("ControlName");
DropDownList ddl =(DropDownList) grd.Rows[rowIndex].FindControl("ControlName");
ddl .DataSource = dt2;
ddl .DataValueField = "value";
ddl .DataTextField = "Text";
ddl .DataBind();
ddl .Items.Insert(0, new ListItem("-- Select --", "-1"));
TextBox box3 = (TextBox) grd.Rows[rowIndex].Cells[3].FindControl("ControlName");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i]["ColumnName"] = box0.Text;
dtCurrentTable.Rows[i][" ColumnName "] = box1.Text;
dtCurrentTable.Rows[i][" ColumnName "] = ddl.SelectedItem.Text;
dtCurrentTable.Rows[i][" ColumnName "] = box3.Text;
rowIndex++;
}
//
dtCurrentTable.Rows.InsertAt(drCurrentRow, 0);
ViewState["CurrentTable"] = dtCurrentTable;
grd.DataSource = dtCurrentTable;
grd .EditIndex = 0;
grd .DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
BindDropDownlist();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//extract the TextBox values
TextBox box0 = ( TextBox )grd.Rows[rowIndex].Cells[0].FindControl("ControlName");
TextBox box1 = ( TextBox )grd.Rows[rowIndex].Cells[1].FindControl(" ControlName ");
TextBox box2 = ( TextBox )grdAdmin.Rows[rowIndex].Cells[2].FindControl("ControlName ");
TextBox box3 = ( TextBox )grdAdmin.Rows[rowIndex].Cells[2].FindControl(" ControlName ");
if (i != 0)
{
DropDownList ddl = (DropDownList)grd.Rows[rowIndex].FindControl("ControlName");
ddlAdminList.SelectedItem.Text = box2.Text;
}
box0.Text = dt.Rows[i]["ColumnName"].ToString();
box1.Text = dt.Rows[i][" ColumnName "].ToString();
box2.Text = dt.Rows[i][" ColumnName "].ToString();
box3.Text = dt.Rows[i][" ColumnName "].ToString();
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column Name", typeof(DataType)));
dt.Columns.Add(new DataColumn(" Column Name ", typeof( DataType )));
dt.Columns.Add(new DataColumn(" Column Name ", typeof( DataType )));
dt.Columns.Add(new DataColumn(" Column Name ", typeof( DataType )));
dr = dt.NewRow();
GridBind();
}
}
private void GridBind()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Connection string");
SqlDataAdapter dap = new SqlDataAdapter("Store Procedure", con);
dap.SelectCommand.CommandType = CommandType.StoredProcedure;
dap.Fill(ds);
grd.DataSource = ds;
grd.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
DataTable dt2 = new DataTable();
SqlConnection con = new SqlConnection("Connectionstring");
SqlCommand cmd = new SqlCommand("spName", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
adp.Fill(ds);
dt2 = ds.Tables[0];
for (int i = 0; i <= dtCurrentTable.Rows.Count-1; i++)
{
//extract the TextBox values
TextBox box0 = (TextBox)grd.Rows[rowIndex].Cells[0].FindControl("ControlName");
TextBox box1 = (TextBox) grd.Rows[rowIndex].Cells[1].FindControl("ControlName");
DropDownList ddl =(DropDownList) grd.Rows[rowIndex].FindControl("ControlName");
ddl .DataSource = dt2;
ddl .DataValueField = "value";
ddl .DataTextField = "Text";
ddl .DataBind();
ddl .Items.Insert(0, new ListItem("-- Select --", "-1"));
TextBox box3 = (TextBox) grd.Rows[rowIndex].Cells[3].FindControl("ControlName");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i]["ColumnName"] = box0.Text;
dtCurrentTable.Rows[i][" ColumnName "] = box1.Text;
dtCurrentTable.Rows[i][" ColumnName "] = ddl.SelectedItem.Text;
dtCurrentTable.Rows[i][" ColumnName "] = box3.Text;
rowIndex++;
}
//
dtCurrentTable.Rows.InsertAt(drCurrentRow, 0);
ViewState["CurrentTable"] = dtCurrentTable;
grd.DataSource = dtCurrentTable;
grd .EditIndex = 0;
grd .DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
BindDropDownlist();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//extract the TextBox values
TextBox box0 = ( TextBox )grd.Rows[rowIndex].Cells[0].FindControl("ControlName");
TextBox box1 = ( TextBox )grd.Rows[rowIndex].Cells[1].FindControl(" ControlName ");
TextBox box2 = ( TextBox )grdAdmin.Rows[rowIndex].Cells[2].FindControl("ControlName ");
TextBox box3 = ( TextBox )grdAdmin.Rows[rowIndex].Cells[2].FindControl(" ControlName ");
if (i != 0)
{
DropDownList ddl = (DropDownList)grd.Rows[rowIndex].FindControl("ControlName");
ddlAdminList.SelectedItem.Text = box2.Text;
}
box0.Text = dt.Rows[i]["ColumnName"].ToString();
box1.Text = dt.Rows[i][" ColumnName "].ToString();
box2.Text = dt.Rows[i][" ColumnName "].ToString();
box3.Text = dt.Rows[i][" ColumnName "].ToString();
rowIndex++;
}
}
}
}
No comments:
Post a Comment