Steps:
1. Create a asp.net web application
2. Create a global DataTable globalDataTable
3. To create a DataTable first time I have created the following function
private DataTable CreateDataTable()
{
DataTable dtSample = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "Id";
dtSample.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "FirstName";
dtSample.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "LastName";
dtSample.Columns.Add(myDataColumn);
return dtSample;
}
4. To create a new row in the gridview the following function has been created :
private void AddNewRow(string firstname, string lastname, DataTable myTable)
6. To Edit and Delete from the gridview the following event has been added to the gridview
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
7. We have saved the data on the Submit button click.
{
if (Convert.ToString(dtSample.Rows[iCount]["Id"]) == lblMsg.Text)
{
dtSample.Rows[iCount]["FirstName"] = txtFirstName.Text;
dtSample.Rows[iCount]["LastName"] = txtLastName.Text;
}
}
}
else
{
AddNewRow(this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim(), (DataTable)Session["dtSample"]);
}
this.GridView1.DataSource = ((DataTable)Session["dtSample"]).DefaultView; this.GridView1.DataBind();
this.txtFirstName.Text = "";
this.txtLastName.Text = "";
this.lblMsg.Text = "";
}
}
Download: