Hi there.
If I am not wrong then you are using data table to bind your gridview. While fetching the data from database and storing in the data table just add a "OPERATION" column to the data table..
What you need to do is, add a bit value to recognize the row operation. For example for adding new values just insert 0 in that column and store in the datatable. After doing all the necessary operation you can find the values which to be updated and which to be added..
Here is the sample code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_objEmpDetails = new clsEmpDetails();
_dtEmpDetails = _objEmpDetails.fnGetDetails();
_dtEmpDetails.Columns.Add("Operation", typeof(string));
Session["EmpDetails"] = _dtEmpDetails;
fnBindEmpDetails();
}
}
private void fnBindEmpDetails()
{
_dtEmpDetails = new DataTable();
_dtEmpDetails = Session["EmpDetails"] as DataTable;
gvEmpDetails.DataSource = _dtEmpDetails;
gvEmpDetails.DataBind();
}
protected void gvEmpDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNew")
{
_dtEmpDetails = new DataTable();
_dtEmpDetails = Session["EmpDetails"] as DataTable;
string strID = ((TextBox)gvEmpDetails.FooterRow.Cells[0].FindControl("txtInsID")).Text;
string strName = ((TextBox)gvEmpDetails.FooterRow.Cells[1].FindControl("txtInsName")).Text;
string strAddress = ((TextBox)gvEmpDetails.FooterRow.Cells[1].FindControl("txtInsAddress")).Text;
_dtEmpDetails.Rows.Add(strID, strName, strAddress, "0");
Session["EmpDetails"] = _dtEmpDetails;
fnBindEmpDetails();
}
}