datagrid binding
<pre><code>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class OrderMaster : System.Web.UI.Page
{
    BLLOrderMaster objMaster = new BLLOrderMaster();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          SetInitialRow();

      string OrderNumber = objMaster.SelectDetails().ToString();
      if (OrderNumber != "")
      {
          txtOrderNo.Text = OrderNumber.ToString();
          txtOrderDate.Focus();
      }
    }
}

private void InsertEmptyRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;

    dt.Columns.Add(new DataColumn("ItemCode", typeof(string)));
    dt.Columns.Add(new DataColumn("Description", typeof(string)));
    dt.Columns.Add(new DataColumn("Unit", typeof(string)));
    dt.Columns.Add(new DataColumn("Qty", typeof(string)));
    dt.Columns.Add(new DataColumn("Rate", typeof(string)));
    dt.Columns.Add(new DataColumn("Disc", typeof(string)));
    dt.Columns.Add(new DataColumn("Amount", typeof(string)));


    for (int i = 0; i &lt; 5; i++)
    {
        dr = dt.NewRow();
        dr["ItemCode"] = string.Empty;
        dr["Description"] = string.Empty;
        dr["Unit"] = string.Empty;
        dr["Qty"] = string.Empty;
        dr["Rate"] = string.Empty;
        dr["Disc"] = string.Empty;
        dr["Amount"] = string.Empty;
        dt.Rows.Add(dr);
    }


    //GridView1.DataSource = dt;
    //GridView1.DataBind();
}

private void SetInitialRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("ItemCode", typeof(string)));
    dt.Columns.Add(new DataColumn("Description", typeof(string)));
    dt.Columns.Add(new DataColumn("Unit", typeof(string)));
    dt.Columns.Add(new DataColumn("Qty", typeof(string)));
    dt.Columns.Add(new DataColumn("Rate", typeof(string)));
    dt.Columns.Add(new DataColumn("Disc", typeof(string)));
    dt.Columns.Add(new DataColumn("Amount", typeof(string)));

    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dr["ItemCode"] = string.Empty;
    dr["Description"] = string.Empty;
    dr["Unit"] = string.Empty;
    dr["Qty"] = string.Empty;
    dr["Rate"] = string.Empty;
    dr["Disc"] = string.Empty;
    dr["Amount"] = string.Empty;
    dt.Rows.Add(dr);

    //Store DataTable
    ViewState["OrderDetails"] = dt;

    Gridview1.DataSource = dt;
    Gridview1.DataBind();
}

protected void AddNewRowToGrid()
{
    int rowIndex = 0;

    if (ViewState["OrderDetails"] != null)
    {
        DataTable dtCurrentTable = (DataTable)ViewState["OrderDetails"];
        DataRow drCurrentRow = null;
        if (dtCurrentTable.Rows.Count &gt; 0)
        {
            for (int i = 1; i &lt;= dtCurrentTable.Rows.Count; i++)
            {
                //extract the TextBox values

                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemCode");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtdescription");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtunit");
                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txtqty");
                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtRate");
                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txtdisc");
                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txtamount");


                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["RowNumber"] = i + 1;
                drCurrentRow["ItemCode"] = box1.Text;
                drCurrentRow["Description"] = box2.Text;
                drCurrentRow["Unit"] = box3.Text;
                drCurrentRow["Qty"] = box4.Text;
                drCurrentRow["Rate"] = box5.Text;
                drCurrentRow["Disc"] = box6.Text;
                drCurrentRow["Amount"] = box7.Text;
                rowIndex++;
            }

            //add new row to DataTable
            dtCurrentTable.Rows.Add(drCurrentRow);

            //Store the current data to ViewState
            ViewState["OrderDetails"] = dtCurrentTable;

            //Rebind the Grid with the current data
            Gridview1.DataSource = dtCurrentTable;
            Gridview1.DataBind();
        }
    }
    else
    {
        //
    }

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

private void SetPreviousData()
{
    int rowIndex = 0;
    if (ViewState["OrderDetails"] != null)
    {
        DataTable dt = (DataTable)ViewState["OrderDetails"];
        if (dt.Rows.Count &gt; 0)
        {
            for (int i = 1; i &lt; dt.Rows.Count; i++)
            {
                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtItemCode");
                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtdescription");
                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtunit");
                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txtqty");
                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtRate");
                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("txtdisc");
                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[7].FindControl("txtamount");

                box1.Text = dt.Rows[i]["ItemCode"].ToString();
                box2.Text = dt.Rows[i]["Description"].ToString();
                box3.Text = dt.Rows[i]["Unit"].ToString();
                box4.Text = dt.Rows[i]["Qty"].ToString();
                box5.Text = dt.Rows[i]["Rate"].ToString();
                box6.Text = dt.Rows[i]["Disc"].ToString();
                box7.Text = dt.Rows[i]["Amount"].ToString();

                rowIndex++;
            }
            dt.AcceptChanges();
        }
        ViewState["OrderDetails"] = dt;
    }
}

protected void BindOrderDetails()
{
    DataTable dtOrderDetails = new DataTable();
    if (ViewState["OrderDetails"] != null)
    {
        dtOrderDetails = (DataTable)ViewState["OrderDetails"];
    }
    else
    {
        dtOrderDetails.Columns.Add("");
        dtOrderDetails.Columns.Add("");
        dtOrderDetails.Columns.Add("");
        dtOrderDetails.Columns.Add("");
        dtOrderDetails.Columns.Add("");
        dtOrderDetails.Columns.Add("");
        dtOrderDetails.AcceptChanges();
        DataRow dr = dtOrderDetails.NewRow();
        dtOrderDetails.Rows.Add(dr);
        ViewState["OrderDetails"] = dtOrderDetails;
    }
    if (dtOrderDetails != null)
    {
        Gridview1.DataSource = dtOrderDetails;
        Gridview1.DataBind();
        if (Gridview1.Rows.Count &gt; 0)
        {
            ((LinkButton)Gridview1.Rows[Gridview1.Rows.Count - 1].FindControl("btnDelete")).Visible = false;
        }
    }
}

protected void btnSave_Click(object sender, EventArgs e)
{
    if (txtOrderDate.Text != "" &amp;&amp; txtOrderNo.Text != "" &amp;&amp; txtPartyName.Text != "" &amp;&amp; txttotalAmount.Text !="")
    {
        BLLOrderMaster bllobj = new BLLOrderMaster();
        DataTable dtdetails = new DataTable();
        UpdateItemDetailRow();
        dtdetails = (DataTable)ViewState["OrderDetails"];
        SetValues(bllobj);
        int k = 0;
        k = bllobj.Insert_Update_Delete(1, bllobj, dtdetails);
        if (k &gt; 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Login Denied", "&lt;Script&gt;alert('Order Code Alraddy Exist');&lt;/Script&gt;", false);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Login Denied", "&lt;Script&gt;alert('Record Saved Successfully');&lt;/Script&gt;", false);
        }
        dtdetails.Clear();
        SetInitialRow();
        txttotalAmount.Text = "";
        txtOrderNo.Text = "";
        txtPartyName.Text = "";
        txtOrderDate.Text = "";
        txttotalQty.Text = "";
        string OrderNumber = objMaster.SelectDetails().ToString();
        if (OrderNumber != "")
        {
            txtOrderNo.Text = OrderNumber.ToString();
            txtOrderDate.Focus();
        }

    }
    else
    {
        txtOrderNo.Text = "";
    }
}

public void SetValues(BLLOrderMaster bllobj)
{
    if (txtOrderNo.Text != null &amp;&amp; txtOrderNo.Text.ToString() != "")
    {
        bllobj.OrNumber = Convert.ToInt16(txtOrderNo.Text);
    }
    if (txtOrderDate.Text != null &amp;&amp; txtOrderDate.Text.ToString() != "")
    {
        bllobj.Date = DateTime.Parse(txtOrderDate.Text.ToString()).ToString("dd/MM/yyyy");
    }
    if (txtPartyName.Text != null &amp;&amp; txtPartyName.Text.ToString() != "")
    {
        bllobj.PartyName = txtPartyName.Text;
    }
    bllobj.TotalBillAmount = txttotalAmount.Text == "" ? 0 : int.Parse(txttotalAmount.Text);
    bllobj.TotalQty = txttotalQty.Text == "" ? 0 : int.Parse(txttotalQty.Text);
}

protected void txtdisc_TextChanged(object sender, EventArgs e)
{
    double total = 0;
    double totalqty = 0;
    foreach (GridViewRow dgvr in Gridview1.Rows)
    {
        TextBox tb = (TextBox)dgvr.Cells[7].FindControl("txtamount");
        double sum;
        if (double.TryParse(tb.Text.Trim(), out sum))
        {
            total += sum;
        }
        TextBox tb1 = (TextBox)dgvr.Cells[4].FindControl("txtqty");
        double qtysum;
        if (double.TryParse(tb1.Text.Trim(), out qtysum))
        { 
            totalqty += qtysum;
        }
    }
    txttotalAmount.Text = total.ToString();
    txttotalQty.Text = totalqty.ToString();
    AddNewRowToGrid();
    Gridview1.TabIndex = 1;
}

public void UpdateItemDetailRow()
{
    DataTable dt = new DataTable();
    if (ViewState["OrderDetails"] != null)
    {
        dt = (DataTable)ViewState["OrderDetails"];
    }
    if (dt.Rows.Count &gt; 0)
    {
        for (int i = 0; i &lt; Gridview1.Rows.Count; i++)
        {
            dt.Rows[i]["ItemCode"] = (Gridview1.Rows[i].FindControl("txtItemCode") as TextBox).Text.ToString();
            if (dt.Rows[i]["ItemCode"].ToString() == "")
            {
                dt.Rows[i].Delete();
                break;
            }
            else
            {
                dt.Rows[i]["Description"] = (Gridview1.Rows[i].FindControl("txtdescription") as TextBox).Text.ToString();
                dt.Rows[i]["Unit"] = (Gridview1.Rows[i].FindControl("txtunit") as TextBox).Text.ToString();
                dt.Rows[i]["Qty"] = (Gridview1.Rows[i].FindControl("txtqty") as TextBox).Text.ToString();
                dt.Rows[i]["Rate"] = (Gridview1.Rows[i].FindControl("txtRate") as TextBox).Text.ToString();
                dt.Rows[i]["Disc"] = (Gridview1.Rows[i].FindControl("txtdisc") as TextBox).Text.ToString();
                dt.Rows[i]["Amount"] = (Gridview1.Rows[i].FindControl("txtamount") as TextBox).Text.ToString();
            }
        }
        dt.AcceptChanges();
    }
    ViewState["OrderDetails"] = dt;
}
</code></pre>

<p>}</p>
