private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
gridbill.DataSource = dt;
gridbill.DataBind();
}
private void AddNewRowToGrid()
{
int rowindex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)gridbill.Rows[rowindex].Cells[1].FindControl("TextBox2");
DropDownList box2 = (DropDownList)gridbill.Rows[rowindex].Cells[2].FindControl("DropDownList1");
TextBox box3 = (TextBox)gridbill.Rows[rowindex].Cells[3].FindControl("TextBox3");
//(DropDownList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("drpQualification");
//drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.SelectedValue;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
rowindex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
gridbill.DataSource = dtCurrentTable;
gridbill.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)gridbill.Rows[rowIndex].Cells[1].FindControl("TextBox2");
DropDownList box2 = (DropDownList)gridbill.Rows[rowIndex].Cells[2].FindControl("DropDownList1");
TextBox box3 = (TextBox)gridbill.Rows[rowIndex].Cells[3].FindControl("TextBox3");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.SelectedValue = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
protected void btnadd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex + 1;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data
dt.Rows.Remove(dt.Rows[rowID]);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
gridbill.DataSource = dt;
gridbill.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData();
}

Kunal VaishyaPosted Dec 18, 2014, 8:16 AM
Start doing delete from Maximum to Minimum,
example
for (int i = dtCurrentTable.Rows.Count; i > 0 ; i--)