Click here to Skip to main content
15,996,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here i am using code but gridview adding new rocords overriding old records Please help me out...
thank you.. :)
C#
private void SetInitialRow()
       {

           DataTable datatable = new DataTable();

           datatable.Columns.Add("SlNo", typeof(string));
           datatable.Columns.Add("ReceiptNo", typeof(string));
           datatable.Columns.Add("ItemName", typeof(string));
           datatable.Columns.Add("UOM", typeof(string));
           datatable.Columns.Add("Description", typeof(string));
           datatable.Columns.Add("Rate", typeof(string));
           datatable.Columns.Add("Quatity", typeof(string));
           datatable.Columns.Add("Amount", typeof(string));
           DataRow dr = null;

           dr = datatable.NewRow();

           dr["SlNo"] = "1";
           dr["ReceiptNo"] = string.Empty;// txtRcptHdNo.Text;
           dr["ItemName"] = string.Empty;// ddlRcptDtlsItem.SelectedItem.Text;
           dr["UOM"] = string.Empty;// txtRcptDtlsUOM.Text;
           dr["Description"] = string.Empty;// txtRcptDtlsDescr.Text;
           dr["Rate"] = string.Empty;// txtRcptDtlsRt.Text;
           dr["Quatity"] = string.Empty;// txtRcptDtlsQty.Text;
           dr["Amount"] = string.Empty;// txtRcptDtlsAmount.Text;

           datatable.Rows.Add(dr);
           //dr = dt.NewRow();

           //Store the DataTable in ViewState
           ViewState["CurrentTable"] = datatable;

           dgvRcptDtls.DataSource = datatable;
           dgvRcptDtls.DataBind();
       }


C#
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++)
            {

                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["SlNo"] = i + 1;

                dtCurrentTable.Rows[i - 1]["ReceiptNo"] = txtRcptHdNo.Text;
                dtCurrentTable.Rows[i - 1]["ItemName"] = ddlRcptDtlsItem.SelectedItem.Text;
                dtCurrentTable.Rows[i - 1]["UOM"] = txtRcptDtlsUOM.Text;
                dtCurrentTable.Rows[i - 1]["Description"] = txtRcptDtlsDescr.Text;
                dtCurrentTable.Rows[i - 1]["Rate"] = txtRcptDtlsRt.Text;
                dtCurrentTable.Rows[i - 1]["Quatity"] = txtRcptDtlsQty.Text;
                dtCurrentTable.Rows[i - 1]["Amount"] = txtRcptDtlsAmount.Text;

                rowIndex++;
            }
            dtCurrentTable.Rows.Add(drCurrentRow);
            ViewState["CurrentTable"] = dtCurrentTable;

            dgvRcptDtls.DataSource = dtCurrentTable;
            dgvRcptDtls.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null");
    }

    //Set Previous Data on Postbacks
    SetPreviousData();
}


C#
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++)
            {

                dt.Rows[i]["ReceiptNo"].ToString();// = txtRcptHdNo.Text;
                dt.Rows[i]["ItemName"].ToString();// = ddlRcptDtlsItem.SelectedItem.Text;
                dt.Rows[i]["UOM"].ToString();// = txtRcptDtlsUOM.Text;
                dt.Rows[i]["Description"].ToString();// = txtRcptDtlsDescr.Text;
                dt.Rows[i]["Rate"].ToString();// = txtRcptDtlsRt.Text;
                dt.Rows[i]["Quatity"].ToString();// = txtRcptDtlsQty.Text;
                dt.Rows[i]["Amount"].ToString();// = txtRcptDtlsAmount.Text;

                rowIndex++;
            }
        }
    }
}


C#
protected void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewRowToGrid();
           
        }

C#
page_load()// event
{
SetInitialRow()
}
Posted
Updated 18-Oct-13 21:31pm
v2

1 solution

Use this code


--------------------------------------------------------------------------------------
private void AddNewRowToGrid()
        {
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;

                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["SlNo"] = dtCurrentTable.Rows.Count + 1;
                drCurrentRow["ReceiptNo"] = txtRcptHdNo.Text;
                drCurrentRow["ItemName"] = ddlRcptDtlsItem.SelectedItem.Text;
                drCurrentRow["UOM"] = txtRcptDtlsUOM.Text;
                drCurrentRow["Description"] = txtRcptDtlsDescr.Text;
                drCurrentRow["Rate"] = txtRcptDtlsRt.Text;
                drCurrentRow["Quatity"] = txtRcptDtlsQty.Text;
                drCurrentRow["Amount"] = txtRcptDtlsAmount.Text;

                dtCurrentTable.Rows.Add(drCurrentRow);

                ViewState["CurrentTable"] = dtCurrentTable;
                dgvRcptDtls.DataSource = dtCurrentTable;
                dgvRcptDtls.DataBind();
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900