locked
I want sum of net amount from grid view. RRS feed

  • Question

  • User-756749920 posted

    Here's my code

    code.aspx

    <asp:GridView ID="grvPurchase" class="tables" ShowFooter="True" runat="server" AutoGenerateColumns="False" GridLines="None" Width="741px" OnRowDataBound="grvPurchase_RowDataBound" OnSelectedIndexChanged="grvPurchase_SelectedIndexChanged" OnRowDeleting="grvPurchase_RowDeleting" OnRowCreated="grvPurchase_RowCreated" OnRowUpdating="grvPurchase_RowUpdating" >
    <Columns>


    <asp:TemplateField HeaderText="SlNo">
    <ItemTemplate>

    <asp:TextBox ID="txtslno" class="form-control responsive-textbox" Text=' <%#Container.DataItemIndex+1 %>' style="width:45px;" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Product Code">
    <ItemTemplate>
    <asp:DropDownList ID="ddlcode" runat="server" class="form-control responsive-textbox" style="width:110px;height:32px;" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlcode_SelectedIndexChanged" >
    <asp:ListItem></asp:ListItem>
    </asp:DropDownList>
    <!-- <asp:TextBox ID="txtcode" class="form-control" style="width:80px;" runat="server" />-->
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Product Name">
    <ItemTemplate>

    <asp:TextBox ID="txtName" class="form-control responsive-textbox" style="width:175px;" runat="server" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField Headertext="HSN Number">
    <ItemTemplate>

    <asp:TextBox ID="txtHsn" class="form-control responsive-textbox" style="width:110px;" runat="server" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Unit">
    <ItemTemplate>

    <asp:TextBox ID="txtun" class="form-control responsive-textbox" style="width:50px;" runat="server" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Quantity">
    <ItemTemplate>

    <asp:TextBox ID="txt_Qty" class="form-control responsive-textbox" ValidationGroup="valid_1" style="width:60px;" runat="server" AutoPostBack="True" OnTextChanged="txt_Qty_TextChanged" />
    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Unit Price">
    <ItemTemplate>

    <asp:TextBox ID="txtunitt" class="form-control responsive-textbox" style="width:80px;" runat="server" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Discount %">
    <ItemTemplate>

    <asp:TextBox ID="txtdiscountt" class="form-control responsive-textbox" style="width:60px;" runat="server" AutoPostBack="True" OnTextChanged="txtdiscountt_TextChanged" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Tax Name">
    <ItemTemplate>
    <!--<asp:TextBox ID="txttype" class="form-control" style="width:80px;" runat="server" />-->
    <asp:DropDownList ID="ddlTaxtype" class="form-control responsive-textbox" style="width:150px;height:32px;" runat="server" AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="ddlTaxtype_SelectedIndexChanged" >
    <asp:ListItem ></asp:ListItem>

    </asp:DropDownList>
    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Tax %">
    <ItemTemplate>

    <asp:TextBox ID="txtgstt" class="form-control responsive-textbox" style="width:90px;" runat="server" />
    <!-- <asp:DropDownList ID="ddlTpercent" class="form-control" style="width:90px;" AppendDataBoundItems="True" AutoPostBack="True" runat="server" OnSelectedIndexChanged="ddlTpercent_SelectedIndexChanged">
    <asp:ListItem ></asp:ListItem>
    </asp:DropDownList>-->
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Tax Amount">
    <ItemTemplate>

    <asp:TextBox ID="txtgstAmount" class="form-control responsive-textbox" style="width:100px;" runat="server" />
    </ItemTemplate>

    </asp:TemplateField>
    <asp:TemplateField HeaderText="Net Amount">
    <ItemTemplate>

    <asp:TextBox ID="txtnett" class="form-control responsive-textbox" style="width:90px;" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" class="btn btn-default" OnClick ="LinkButton1_Click"><span style="color:red;font-size:medium">x</span></asp:LinkButton>
    </ItemTemplate>
    <FooterStyle HorizontalAlign="right" />

    <FooterTemplate>

    <asp:Button ID="ButtonAdd" runat="server" class="btn btn-primary" Text="Add" ValidationGroup="valid_1" OnClick="ButtonAdd_Click" />

    </FooterTemplate>
    </asp:TemplateField>


    </Columns>
    </asp:GridView>

    <table class="table">

    <tr>
    <td align="right">Discount:</td>
    <td><asp:TextBox ID="discount" class="form-control" placeholder="Discount" AutoComplete="off" Autopostback="true" style="width:120px;align-content:right;" runat="server" OnTextChanged="discount_TextChanged"></asp:TextBox>
    </td>

    <td align="right">Total Amount:</td>
    <td>
    <asp:TextBox ID="total_amount" class="form-control" placeholder="Total Amount" AutoComplete="off" style="width:120px;" runat="server"></asp:TextBox></td>
    <td></td>
    <td></td>

    </tr>
    <tr>
    <td style=" font-stretch:expanded;" align="right">Round Off:</td>
    <td><asp:TextBox ID="roundoff" placeholder="Round" style="width:120px;" AutoComplete="off" class="form-control" runat="server"></asp:TextBox></td>
    <td style=" font-stretch:expanded;" align="right">Freight:</td>
    <td><asp:TextBox ID="freight" Autopostback="true" class="form-control" placeholder="Charges" AutoComplete="off" style="width:120px;" runat="server" OnTextChanged="freight_TextChanged"></asp:TextBox> </td>
    <td></td>
    <td></td>
    </tr>
    <tr id="show1" style="display:none;">

    <td align="right">Old Balance:</td>
    <td style="width:150px;">
    <asp:TextBox ID="oldbalance" class="form-control" value="0" placeholder="Old Balance" AutoComplete="off" style="width:120px;" runat="server"></asp:TextBox>
    </td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td align="right">Grant Total:</td>
    <td width="150">
    <asp:TextBox ID="gtotalprice" class="form-control" placeholder="Grant Total" style="width:120px;" AutoComplete="off" runat="server"></asp:TextBox>
    </td>
    <td align="right">Paid Amount:</td>
    <td width="150"><asp:TextBox ID="paidamount" class="form-control" Autopostback="true" placeholder="Paid Amount" AutoComplete="off" style="width:120px;" runat="server" OnTextChanged="paidamount_TextChanged"></asp:TextBox>
    </td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td align="right">Balance Amount:</td>
    <td width="150">
    <asp:TextBox ID="balance_amount" class="form-control" style="width:120px;" placeholder="Balance" AutoComplete="off" runat="server"></asp:TextBox>
    </td>
    <td align="right">Payment Method:</td>
    <td width="150">
    <asp:DropDownList ID="paytype" class="form-control" style="width:120px;" runat="server">
    <asp:ListItem Value="CASH">Cash</asp:ListItem>
    <asp:ListItem Value="BANK">Bank</asp:ListItem>
    </asp:DropDownList>
    </td>
    <td align="right">Bill Type:</td>
    <td width="150">
    <asp:DropDownList ID="billtype" class="form-control" style="width:120px;" runat="server">
    <asp:ListItem Value="1">Tax Invoice</asp:ListItem>
    <asp:ListItem Value="3">Customer Invoice</asp:ListItem>
    </asp:DropDownList>
    </td>
    </tr>

    </table>
    <div class="form-group" align="right" style="padding-right:40px;">
    <asp:Label for="input-help-block" ID="Label1" class="col-sm-2 control-label" runat="server" Text=""></asp:Label>
    <asp:Button ID="btn_save" runat="server" Text="Save & Print" class="btn btn-primary" ValidationGroup="valid" onclick="btn_save_Click"/>

    Code.aspx.cs

    if (!IsPostBack)
    {
    if (Request.QueryString["billno"] != null) invoice_no = Request.QueryString["billno"].ToString();
    //dt1 = db.select("select max(be_billid)from tbl_purchasebill");
    //bill = int.Parse(dt1.Rows[0][0].ToString());
    //bill = bill + 1;
    // billno.Text = bill.ToString();
    dt2 = db.select("SELECT COALESCE (MAX(be_billnumber),0) FROM tbl_purchasebill WHERE company_id='" + Session["Newc"] + "' AND account_id= '" + Session["uid"] + "'");
    if (dt2.Rows.Count > 0)
    {
    bill = int.Parse(dt2.Rows[0][0].ToString());
    bill = bill + 1;

    }
    else
    {
    bill = 0;
    bill = bill + 1;

    }
    if (invoice_no == "O")
    {
    billno.Text = bill.ToString();


    }

    else if (invoice_no == "N")
    {
    billno.Text = (bill + 1).ToString();
    }
    else
    {
    billno.Text = bill.ToString();
    }
    //billno.Text = bill.ToString();
    date.Value = System.DateTime.Today.ToShortDateString();
    time.Text = System.DateTime.Now.ToShortTimeString();
    SetInitialRow();
    BindCustomer();
    // AddNewRowToGrid();
    //SetPreviousData();
    //BindDrop();

    }
    }

    public void BindCustomer()
    {
    db = new DBConnection();
    dt = db.select("SELECT rs_name FROM vm_supplier where company_id='" + Session["Newc"] + "' AND account_id ='" + Session["uid"] + "'");
    ddlcustomer.DataSource = dt;
    ddlcustomer.DataTextField = "rs_name";
    ddlcustomer.DataValueField = "rs_name";
    ddlcustomer.DataBind();
    }

    protected void ddlcustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
    dt = db.select("select * from vm_supplier where rs_name='" + ddlcustomer.SelectedValue + "'");
    if (dt.Rows.Count > 0)
    {
    phone_N.Text = dt.Rows[0]["rs_phone"].ToString();
    vehicle_number.Text = dt.Rows[0]["rs_suppVehicle"].ToString();
    tin_number.Text = dt.Rows[0]["rs_tinnum"].ToString();
    supplyplace.SelectedValue = dt.Rows[0]["rs_statecode"].ToString();
    }
    }

    private void SetInitialRow()

    {
    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)));

    dt.Columns.Add(new DataColumn("Column4", typeof(string)));

    dt.Columns.Add(new DataColumn("Column5", typeof(string)));

    dt.Columns.Add(new DataColumn("Column6", typeof(string)));

    dt.Columns.Add(new DataColumn("Column7", typeof(string)));

    dt.Columns.Add(new DataColumn("Column8", typeof(string)));

    dt.Columns.Add(new DataColumn("Column9", typeof(string)));

    dt.Columns.Add(new DataColumn("Column10", typeof(string)));

    dt.Columns.Add(new DataColumn("Column11", typeof(string)));

    dr = dt.NewRow();

    dr["RowNumber"] = 1;

    dr["Column1"] = string.Empty;

    dr["Column2"] = string.Empty;

    dr["Column3"] = string.Empty;

    dr["Column4"] = string.Empty;

    dr["Column5"] = string.Empty;

    dr["Column6"] = string.Empty;

    dr["Column7"] = string.Empty;

    dr["Column8"] = string.Empty;

    dr["Column9"] = string.Empty;


    dr["Column10"] = string.Empty;

    dr["Column11"] = string.Empty;


    dt.Rows.Add(dr);

    //dr = dt.NewRow();

    //Store the DataTable in ViewState

    ViewState["CurrentTable"] = dt;

    grvPurchase.DataSource = dt;

    grvPurchase.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

    DropDownList box1 = (DropDownList)grvPurchase.Rows[rowIndex].Cells[1].FindControl("ddlcode");

    TextBox box2 = (TextBox)grvPurchase.Rows[rowIndex].Cells[2].FindControl("txtName");

    TextBox box3 = (TextBox)grvPurchase.Rows[rowIndex].Cells[3].FindControl("txtHsn");

    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].Cells[4].FindControl("txtun");

    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].Cells[5].FindControl("txt_Qty");

    TextBox box6 = (TextBox)grvPurchase.Rows[rowIndex].Cells[6].FindControl("txtunitt");

    TextBox box7 = (TextBox)grvPurchase.Rows[rowIndex].Cells[7].FindControl("txtdiscountt");

    DropDownList box8 = (DropDownList)grvPurchase.Rows[rowIndex].Cells[8].FindControl("ddlTaxtype");

    TextBox box9 = (TextBox)grvPurchase.Rows[rowIndex].Cells[9].FindControl("txtgstt");

    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].Cells[10].FindControl("txtgstAmount");

    TextBox box11 = (TextBox)grvPurchase.Rows[rowIndex].Cells[11].FindControl("txtnett");

    if (box1.Text.Length != 0)
    {

    drCurrentRow = dtCurrentTable.NewRow();

    drCurrentRow["RowNumber"] = i + 1;

    dtCurrentTable.Rows[i - 1]["Column1"] = box1.SelectedValue;

    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;

    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;

    dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;

    dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;

    dtCurrentTable.Rows[i - 1]["Column6"] = box6.Text;

    dtCurrentTable.Rows[i - 1]["Column7"] = box7.Text;

    dtCurrentTable.Rows[i - 1]["Column8"] = box8.SelectedValue;

    dtCurrentTable.Rows[i - 1]["Column9"] = box9.Text;

    dtCurrentTable.Rows[i - 1]["Column10"] = box10.Text;

    dtCurrentTable.Rows[i - 1]["Column11"] = box11.Text;


    rowIndex++;
    }

    }
    dtCurrentTable.Rows.Add(drCurrentRow);


    ViewState["CurrentTable"] = dtCurrentTable;

    grvPurchase.DataSource = dtCurrentTable;

    grvPurchase.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++)

    {

    DropDownList box1 = (DropDownList)grvPurchase.Rows[rowIndex].Cells[1].FindControl("ddlcode");

    TextBox box2 = (TextBox)grvPurchase.Rows[rowIndex].Cells[2].FindControl("txtName");

    TextBox box3 = (TextBox)grvPurchase.Rows[rowIndex].Cells[3].FindControl("txtHsn");

    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].Cells[4].FindControl("txtun");

    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].Cells[5].FindControl("txt_Qty");

    TextBox box6 = (TextBox)grvPurchase.Rows[rowIndex].Cells[6].FindControl("txtunitt");

    TextBox box7 = (TextBox)grvPurchase.Rows[rowIndex].Cells[7].FindControl("txtdiscountt");

    DropDownList box8 = (DropDownList)grvPurchase.Rows[rowIndex].Cells[8].FindControl("ddlTaxtype");

    TextBox box9 = (TextBox)grvPurchase.Rows[rowIndex].Cells[9].FindControl("txtgstt");

    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].Cells[10].FindControl("txtgstAmount");

    TextBox box11 = (TextBox)grvPurchase.Rows[rowIndex].Cells[11].FindControl("txtnett");

    box1.SelectedValue = dt.Rows[i]["Column1"].ToString();

    box2.Text = dt.Rows[i]["Column2"].ToString();

    box3.Text = dt.Rows[i]["Column3"].ToString();

    box4.Text = dt.Rows[i]["Column4"].ToString();

    box5.Text = dt.Rows[i]["Column5"].ToString();

    box6.Text = dt.Rows[i]["Column6"].ToString();

    box7.Text = dt.Rows[i]["Column7"].ToString();

    box8.SelectedValue = dt.Rows[i]["Column8"].ToString();

    box9.Text = dt.Rows[i]["Column9"].ToString();

    box10.Text = dt.Rows[i]["Column10"].ToString();

    box11.Text = dt.Rows[i]["Column11"].ToString();


    rowIndex++;

    }


    }

    }

    }
    protected void ButtonAdd_Click(object sender, EventArgs e)

    {
    // DataTable dt = (DataTable)ViewState["CurrentTable"];

    AddNewRowToGrid();
    /* if (dt.Rows.Count > 0)
    {
    double sum = 0;
    //String a = "0";
    for (int i = 0; i < dt.Rows.Count-1; i++)
    {

    double CurrentAmt = Convert.ToDouble((dt.Rows[i]["Column11"] == DBNull.Value) ? 0 : dt.Rows[i]["Column11"]);
    //a = (Convert.ToDecimal(a)+b).ToString();
    sum += CurrentAmt;

    }
    total_amount.Text = sum.ToString();
    roundoff.Text = (Math.Round(sum)).ToString();
    gtotalprice.Text = (Math.Round(sum)).ToString();
    balance_amount.Text = (Math.Round(sum)).ToString();
    }*/

    }

    protected void btn_save_Click(object sender, EventArgs e)
    {

    AddNewRowToGrid();

    // SetPreviousData();

    if (ViewState["CurrentTable"] != null)
    {

    DataTable dt = (DataTable)ViewState["CurrentTable"];
    dt1 = dt.Select("Column1 is not null").CopyToDataTable();
    dt1.Columns.RemoveAt(0);
    // db.BulkInsert(dt3, "tbl_purchasebill_item");
    dt2 = db.select("SELECT COALESCE (MAX(be_billnumber),0) FROM tbl_purchasebill WHERE company_id='" + Session["Newc"] + "' AND account_id='" + Session["uid"] + "'");
    if (dt2.Rows.Count > 0)
    {
    bill = int.Parse(dt2.Rows[0][0].ToString());
    bill = bill + 1;

    }
    else
    {
    bill = 0;
    bill = bill + 1;

    }

    db.insert("INSERT INTO tbl_purchasebill(account_id,company_id,be_billnumber,be_customername,be_customermobile,be_vehicle_number,be_customer_tin_num,be_statecode,be_billdate,be_discount,be_total,be_round,be_coolie,be_gtotal,be_paidamount,be_balance,be_paymethod) values('" + Session["uid"] + "','" + Session["Newc"] + "','" + bill + "','" + ddlcustomer.SelectedValue + "','" + phone_N.Text + "','" + vehicle_number.Text + "','" + tin_number.Text + "','" + supplyplace.SelectedValue + "','" + date.Value + "','" + discount.Text + "','" + total_amount.Text + "','" + roundoff.Text + "','" + freight.Text + "','" + gtotalprice.Text + "','" + paidamount.Text + "','" + balance_amount.Text + "','" + paytype.SelectedValue + "')");

    for (int i = 0; i < dt1.Rows.Count; i++)

    {
    //System.Windows.Forms.MessageBox.Show(dt1.Rows[i]["Column1"].ToString());


    date_t = System.DateTime.Today.ToShortDateString();

    if (dt.Rows.Count > 0)
    {


    if (db.insert("insert into tbl_purchasebill_item(bill_id,company_id,account_id,item_id,item_name,HSN_number,unit,quantity,unit_price,discount,tax_type,tax_percentage,tax_amount,net_amount)values('" + bill + "','" + Session["Newc"] + "' ,'" + Session["uid"] + "','" + dt1.Rows[i]["Column1"] + "','" + dt1.Rows[i]["Column2"].ToString() + "','" + dt1.Rows[i]["Column3"].ToString() + "', '" + dt1.Rows[i]["Column4"].ToString() + "', '" + dt1.Rows[i]["Column5"].ToString() + "', '" + dt1.Rows[i]["Column6"].ToString() + "', '" + dt1.Rows[i]["Column7"].ToString() + "', '" + dt1.Rows[i]["Column8"].ToString() + "', '" + dt1.Rows[i]["Column9"].ToString() + "', '" + dt1.Rows[i]["Column10"].ToString() + "', '" + dt1.Rows[i]["Column11"].ToString() + "' )"))
    {

    DropDownList box1 = (DropDownList)grvPurchase.Rows[i].FindControl("ddlcode");
    TextBox box5 = (TextBox)grvPurchase.Rows[i].FindControl("txt_Qty");
    dt4 = db.select("select stock,purchase_product from vm_product where productcode='" + box1.SelectedValue + "'");
    if (dt4.Rows.Count > 0)
    {
    double qty = Convert.ToDouble(dt4.Rows[0][0].ToString());
    double stocks = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToDouble(box5.Text);

    double result = qty + stocks;

    box5.Text = result.ToString();

    double purchaseproduct = Convert.ToDouble(dt4.Rows[0][1].ToString());
    var result3 = Convert.ToInt32(purchaseproduct + stocks).ToString();

    db.insert("update vm_product set stock='" + result + "', purchase_product='" + result3 + "' where productcode='" + box1.SelectedValue + "'");

    dt2 = db.select("select Purchase_Product,Stock,Product_code from stock_adjustment where account_id = '" + Session["uid"] + "' and company_id = '" + Session["Newc"] + "' ");
    if (dt2.Rows.Count > 0)
    {

    stock1 = int.Parse(dt2.Rows[0]["Stock"].ToString());
    // purchse = int.Parse(dt2.Rows[0]["Purchase_Product"].ToString());

    stock1 = Convert.ToInt16(stock1 + stocks);
    }
    else
    {
    stock1 = 0;
    stock1 = Convert.ToInt16(stock1 + stocks);
    }


    db.insert("insert into stock_adjustment (bill_number,company_id,account_id,Stock_date,time,Product_code,Purchase_Product,Stock,Net_Amount) values ('" + bill + "', '" + Session["Newc"] + "', '" + Session["uid"] + "', '" + date.Value + "','"+time.Text+"','" + dt1.Rows[i]["Column1"] + "','" + dt1.Rows[i]["Column5"].ToString() + "', '" + stock1 + "','" + dt1.Rows[i]["Column11"].ToString() + "')");
    }
    }

    }

    }
    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Added Successfully');window.location.href='purchase.aspx?billno=O';</script>");

    }


    }


    protected void grvPurchase_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    //Find the DropDownList in the Row
    DropDownList ddlcode = (e.Row.FindControl("ddlcode") as DropDownList);
    ddlcode.DataSource = db.select("SELECT productcode FROM vm_product where company_id='" + Session["Newc"] + "' AND account_id = '"+ Session["uid"] + "'");
    ddlcode.DataTextField = "productcode";
    ddlcode.DataValueField = "productcode";
    ddlcode.DataBind();


    DropDownList ddltax = (e.Row.FindControl("ddlTaxtype") as DropDownList);
    ddltax.DataSource = db.select("SELECT tax_code FROM tbl_tax_details");
    ddltax.DataTextField = "tax_code";
    ddltax.DataValueField = "tax_code";
    ddltax.DataBind();

    }

    }


    protected void ddlcode_SelectedIndexChanged(object sender, EventArgs e)
    {
    int rowIndex = ((GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent).RowIndex;

    DataTable dt = (DataTable)ViewState["CurrentTable"];

    //for (int i = 1; i <= grvPurchase.Rows.Count; i++)
    ////foreach (var gridRow in grvPurchase.Rows)
    //{
    DropDownList box1 = (DropDownList)grvPurchase.Rows[rowIndex].FindControl("ddlcode");
    TextBox box2 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtName");
    TextBox box3 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtHsn");
    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtun");
    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txt_Qty");
    TextBox box6 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtunitt");
    DropDownList box7 = (DropDownList)grvPurchase.Rows[rowIndex].FindControl("ddlTaxtype");
    TextBox box8 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstt");
    TextBox box9 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstAmount");
    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtnett");
    // DropDownList ddlgv = e.Rows.FindControl("ddlcode") as DropDownList;
    dt3 = db.select("select * from vm_product where productcode='" + box1.SelectedValue + "'");
    // int qty = 1;
    if (dt3.Rows.Count > 0)
    {

    //int j = dt.Rows.Count +1;
    box2.Text = dt3.Rows[0]["productname"].ToString();
    box3.Text = dt3.Rows[0]["hsnnumber"].ToString();
    box6.Text = dt3.Rows[0]["saleprice"].ToString();
    //box5.Text = dt.Rows[0]["quantity"].ToString();
    box4.Text = dt3.Rows[0]["unit"].ToString();
    box10.Text = dt3.Rows[0]["saleprice"].ToString();



    }
    /*if (dt.Rows.Count > 0)
    {
    double sum = 0;
    //String a = "0";
    for (int i = 0; i < dt.Rows.Count; i++)
    {

    double CurrentAmt = Convert.ToDouble((dt.Rows[i]["column11"] == DBNull.Value) ? 0 : dt.Rows[i]["column11"]);
    //a = (Convert.ToDecimal(a)+b).ToString();
    sum += CurrentAmt;

    }
    total_amount.Text = sum.ToString();
    roundoff.Text = (Math.Round(sum)).ToString();
    gtotalprice.Text = (Math.Round(sum)).ToString();
    balance_amount.Text = (Math.Round(sum)).ToString();
    }*/


    }

    protected void grvPurchase_SelectedIndexChanged(object sender, EventArgs e)
    {
    // DropDownList ddlcode = (grvPurchase.FindControl("ddlcode") as DropDownList);
    // dt = db.select("select * from vm_product where productcode='" + ddlcode.SelectedValue + "'");
    // int qty = 1;
    }

    protected void txt_Qty_TextChanged(object sender, EventArgs e)
    {

    /* DataTable dt = (DataTable)ViewState["CurrentTable"];

    if (dt.Rows.Count > 0)
    {
    double sum = 0;

    for (int i = 0; i < dt.Rows.Count; i++)
    {

    double CurrentAmt = string.IsNullOrEmpty(dt.Rows[i]["Column11"].ToString()) ? 0 : Convert.ToInt32(dt.Rows[i]["Column11"].ToString());
    //a = (Convert.ToDecimal(a)+b).ToString();
    sum += CurrentAmt;

    }
    total_amount.Text = sum.ToString();
    roundoff.Text = (Math.Round(sum)).ToString();
    gtotalprice.Text = (Math.Round(sum)).ToString();
    balance_amount.Text = (Math.Round(sum)).ToString();
    }*/

    int rowIndex = ((GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent).RowIndex;


    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtun");
    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txt_Qty");
    TextBox box6 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtunitt");
    //TextBox box7 = (TextBox)grvPurchase.Rows[0].FindControl("txttype");
    TextBox box8 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstt");
    TextBox box9 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstAmount");
    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtnett");


    if (box9.Text == string.Empty)
    {

    int unit = string.IsNullOrEmpty(box6.Text) ? 0 : Convert.ToInt32(box6.Text);
    int qty = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToInt32(box5.Text);
    Int64 result = unit * qty;
    //var gst = Int32.Parse(box8.SelectedValue);
    //var gstt = ((result * gst) / 100;
    //box9.Text = gstt.ToString();
    box10.Text = result.ToString();
    }

    else if (box9.Text != string.Empty)
    {
    int unit = string.IsNullOrEmpty(box6.Text) ? 0 : Convert.ToInt32(box6.Text);
    int qty = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToInt32(box5.Text);
    Int64 result = unit * qty;
    double gst = string.IsNullOrEmpty(box8.Text) ? 0 : Convert.ToInt32(box8.Text);
    double gstt = ((result * gst) / 100);
    box9.Text = gstt.ToString();
    box10.Text = (result + gstt).ToString();
    }
    else
    {
    int unit = string.IsNullOrEmpty(box6.Text) ? 0 : Convert.ToInt32(box6.Text);
    int qty = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToInt32(box5.Text);
    int result = unit * qty;
    //var gst = Int32.Parse(box8.SelectedValue);
    //var gstt = ((result * gst) / 100);
    //box9.Text = gstt.ToString();
    box10.Text = result.ToString();
    }


    }

    protected void txtdiscountt_TextChanged(object sender, EventArgs e)
    {

    int rowIndex = ((GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent).RowIndex;

    DataTable dt = (DataTable)ViewState["CurrentTable"];

    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtnett");
    TextBox box11 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtdiscountt");
    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtunitt");
    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txt_Qty");


    TextBox txtTaxPercentage = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstt");
    DropDownList box8 = (DropDownList)grvPurchase.Rows[rowIndex].FindControl("ddlTaxtype");
    TextBox txtxTaxAmount = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstAmount");

    int unit = string.IsNullOrEmpty(box4.Text) ? 0 : Convert.ToInt32(box4.Text);
    int qty = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToInt32(box5.Text);

    double result = unit * qty;

    dt1 = db.select("SELECT tax_percentage FROM tbl_tax_details where tax_code='" + box8.SelectedValue + "' ");

    if (dt1.Rows.Count > 0)
    {
    int tax = string.IsNullOrEmpty(dt1.Rows[0][0].ToString()) ? 0 : Convert.ToInt32(dt1.Rows[0][0].ToString());
    txtTaxPercentage.Text = tax.ToString();
    double amount = (result * tax) / 100;
    result += amount;
    txtxTaxAmount.Text = (amount).ToString();
    }

    var discount = string.IsNullOrEmpty(box11.Text) ? 0 : Convert.ToDouble(box11.Text);

    box10.Text = (result - ((result * discount) / 100)).ToString();


    }

    protected void grvPurchase_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
    LinkButton lb = (LinkButton)sender;
    GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
    int rowID = gvRow.RowIndex;
    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 and reset row number
    dt.Rows.Remove(dt.Rows[rowID]);
    ResetRowID(dt);
    }
    }

    //Store the current data in ViewState for future reference
    ViewState["CurrentTable"] = dt;

    //Re bind the GridView for the updated data
    grvPurchase.DataSource = dt;
    grvPurchase.DataBind();
    }

    //Set Previous Data on Postbacks
    SetPreviousData();
    }
    private void ResetRowID(DataTable dt)
    {
    int rowNumber = 1;
    if (dt.Rows.Count > 0)
    {
    foreach (DataRow row in dt.Rows)
    {
    row[0] = rowNumber;
    rowNumber++;
    }
    }
    }

    protected void grvPurchase_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    DataTable dt = (DataTable)ViewState["CurrentTable"];
    LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
    if (lb != null)
    {
    if (dt.Rows.Count > 1)
    {
    if (e.Row.RowIndex == dt.Rows.Count - 1)
    {
    lb.Visible = false;
    }
    }
    else
    {
    lb.Visible = false;
    }
    }
    }
    }


    public void BindDrop()
    {
    int rowIndex = 0;
    for (int i = 1; i <= grvPurchase.Rows.Count; i++)

    {

    DropDownList box8 = (DropDownList)grvPurchase.Rows[rowIndex].Cells[i].FindControl("ddlTpercent");
    DropDownList box9 = (DropDownList)grvPurchase.Rows[rowIndex].Cells[i].FindControl("ddlTaxtype");
    db = new DBConnection();


    }
    }

    protected void ddlTaxtype_SelectedIndexChanged(object sender, EventArgs e)
    {

    int rowIndex = ((GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent).RowIndex;


    DataTable dt = (DataTable)ViewState["CurrentTable"];
    //for (int i = 1; i <= grvPurchase.Rows.Count; i++)

    //{
    //rowIndex= dt.Rows.IndexOf(row);
    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtunitt");
    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txt_Qty");
    TextBox box11 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtdiscountt");
    int unit = string.IsNullOrEmpty(box4.Text) ? 0 : Convert.ToInt32(box4.Text);
    int qty = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToInt32(box5.Text);
    int disc = string.IsNullOrEmpty(box11.Text) ? 0 : Convert.ToInt32(box11.Text);

    double netamount = unit * qty;
    double discount = (netamount * disc) / 100;
    netamount -= discount;

    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstt");
    DropDownList box8 = (DropDownList)grvPurchase.Rows[rowIndex].FindControl("ddlTaxtype");
    TextBox box9 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstAmount");
    TextBox txtTax = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtnett");

    dt1 = db.select("SELECT tax_percentage FROM tbl_tax_details where tax_code='" + box8.SelectedValue + "' ");

    if (dt1.Rows.Count > 0)
    {
    box10.Text = dt1.Rows[0][0].ToString();
    double tax = string.IsNullOrEmpty(dt1.Rows[0][0].ToString()) ? 0 : Convert.ToDouble(dt1.Rows[0][0].ToString());
    double result = (netamount * tax) / 100;
    box9.Text = result.ToString();
    txtTax.Text = (netamount + result).ToString();
    }

    // }

    }


    protected void ddlTpercent_SelectedIndexChanged(object sender, EventArgs e)
    {
    int rowIndex = ((GridViewRow)((DataControlFieldCell)((DropDownList)sender).Parent).Parent).RowIndex;
    //for (int i = 1; i <= grvPurchase.Rows.Count; i++)
    DataTable dt = (DataTable)ViewState["CurrentTable"];
    //{
    TextBox box4 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtunitt");
    TextBox box5 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txt_Qty");
    TextBox box11 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtdiscountt");
    int unit = string.IsNullOrEmpty(box4.Text) ? 0 : Convert.ToInt32(box4.Text);
    int qty = string.IsNullOrEmpty(box5.Text) ? 0 : Convert.ToInt32(box5.Text);
    int disc = string.IsNullOrEmpty(box11.Text) ? 0 : Convert.ToInt32(box11.Text);
    double netamount = unit * qty;
    double discount = (netamount * disc) / 100;
    netamount -= discount;

    TextBox box10 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtnett");
    DropDownList box8 = (DropDownList)grvPurchase.Rows[rowIndex].FindControl("ddlTpercent");
    TextBox box9 = (TextBox)grvPurchase.Rows[rowIndex].FindControl("txtgstAmount");

    var tax = Int32.Parse(box8.SelectedValue);
    box9.Text = ((netamount * tax) / 100).ToString();
    box10.Text = (netamount + ((netamount * tax) / 100)).ToString();

    //}
    //rowIndex++;

    }

    protected void discount_TextChanged(object sender, EventArgs e)
    {

    double dis = string.IsNullOrEmpty(discount.Text) ? 0 : Convert.ToDouble(discount.Text.Trim());
    double paid = string.IsNullOrEmpty(total_amount.Text) ? 0 : Convert.ToDouble(total_amount.Text);
    double amt = ((dis * paid) / 100);
    double result = paid - amt;
    roundoff.Text = (Math.Round(result)).ToString();
    gtotalprice.Text = (Math.Round(result)).ToString();
    balance_amount.Text = (Math.Round(result)).ToString();

    }
    protected void freight_TextChanged(object sender, EventArgs e)
    {

    int coolie = string.IsNullOrEmpty(freight.Text) ? 0 : Convert.ToInt32(freight.Text);
    int total = string.IsNullOrEmpty(roundoff.Text) ? 0 : Convert.ToInt32(roundoff.Text);
    gtotalprice.Text = (coolie + total).ToString();
    balance_amount.Text = (coolie + total).ToString();
    }

    protected void paidamount_TextChanged(object sender, EventArgs e)
    {

    int gtotal = string.IsNullOrEmpty(gtotalprice.Text) ? 0 : Convert.ToInt32(gtotalprice.Text);
    int paid = string.IsNullOrEmpty(paidamount.Text) ? 0 : Convert.ToInt32(paidamount.Text);
    balance_amount.Text = (gtotal - paid).ToString();
    }

    Monday, April 1, 2019 11:11 AM

All replies

  • User288213138 posted

    Hi jibinshaju:

      According to your description, I have made a sum demo. I am not quite clear about what you are going to do. If you want to make a summation, you can try the following code. If this does not meet your needs, please elaborate on your needs.    

     protected void Page_Load(object sender, EventArgs e)
    
            {
    
                if (!IsPostBack)
    
                {
    
                    Bind();
    
                }
    
            }
    
            public string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    
            public void Bind()
    
            {         
    
                using (SqlConnection con = new SqlConnection(constr))
    
                {
    
                    string query = "select top 5 * from Test";
    
                    using (SqlDataAdapter adapter = new SqlDataAdapter(query, con))         
    
                    {                  
    
                        DataSet myds = new DataSet();
    
                        con.Open();
    
                        adapter.Fill(myds, "Test");
    
                        GridView1.DataSource = myds;
    
                        GridView1.DataKeyNames = new string[] { "Id" };
    
                        GridView1.DataBind();
    
                    }
    
                }
    
            }
    
            private double sum = 0; 
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
            {
    
                if (e.Row.RowIndex >= 0)
    
                {
    
                    sum += Convert.ToDouble(e.Row.Cells[6].Text);
    
                }
    
                else if (e.Row.RowType == DataControlRowType.Footer)
    
                {
    
                    e.Row.Cells[5].Text = "total salary:";
    
                    e.Row.Cells[6].Text = sum.ToString();
    
                }
    
    }

    The Result:

    Best Regard

    Sam

    Tuesday, April 2, 2019 6:57 AM