You must Sign In to post a response.
  • Category: ASP.NET

    Need to total in run time in gridview without using rowbound in asp.net

    need to total in run time in gridview without using rowbound in asp.net
  • #759540
    You can do it in the Query by using Sum function or You can Manipulate datatable too Either ways can do it.
    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #759547
    you can also calculate the total in the PreRender event of the Gridview control. Here you have to iterate through each row the gridivew. Find the control which contains the actual value and use it to calculate total. Then in the footer display the total value.

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
    var r = GridView1.Rows;
    decimal total = 0;
    foreach(GridViewRow r1 in r)
    {
    if(r1.RowType == DataControlRowType.DataRow)
    {
    TextBox t = (TextBox)r1.Cells[2].FindControl("txtValue");
    total += Convert.ToDecimal(t.Text);
    }
    }
    //display total value in the footer
    }

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #759552
    sorry your code not works for me mis.priya jain

  • #759560
    Hi

    Try this your code working for total in run time in gridview without using rowbound in asp.net


    decimal total = 0;
    decimal Total3 = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {

    }
    }
    private void BindListView()
    {
    string constr = ConfigurationManager.ConnectionStrings["DBConnection"].ToString();
    using (SqlConnection con = new SqlConnection(constr))
    {
    using (SqlCommand cmd = new SqlCommand())
    {
    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
    {
    cmd.CommandText = "select distinct sectionname,lotno,Productname,mrp from purchase where lotno='" + txtBarcode.Text + "'";
    cmd.Connection = con;
    DataTable dt = new DataTable();
    sda.Fill(dt);
    DataTable products = new DataTable();
    if (ViewState["Products"] == null)
    {
    products.Merge(dt, true);
    }
    else
    {
    products = (DataTable)ViewState["Products"];
    products.Merge(dt, true);
    }
    ViewState["Products"] = products;
    ddsales.DataSource = products;
    ddsales.DataBind();
    if (ViewState["total"] == null || ViewState["total"] == "") { total = 0; ViewState["total"] = 0; }
    total =Convert.ToDecimal(ViewState["total"]) + dt.AsEnumerable().Sum(row => row.Field<decimal>("mrp"));
    ViewState["total"] = total;
    //total = total + Convert.ToDecimal(dt.DefaultView[0]["mrp"]);
    ddsales.FooterRow.Cells[1].Text = "Total";
    ddsales.FooterRow.Cells[1].HorizontalAlign = HorizontalAlign.Right;
    ddsales.FooterRow.Cells[2].Text = total.ToString("N2");
    }
    }
    }
    txtBarcode.Text = "";
    txtBarcode.Focus();
    }

    protected void bt1_Click(object sender, EventArgs e)
    {
    BindListView();
    }

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #759576
    Hello Selvi,

    Is there any problem occurs if you are using GridView RowBound event??

    Refer the below code :

    int amount = 0;
    for(int j = 0; j < StockGridView.Rows.Count; j++)
    {
    amount = amount + Convert.ToInt32(StockGridView.Rows[j].Cells["Price"].Value);
    }
    TextBoxGrandTotal.Text = amount.ToString();


    Hope this willl help you.
    Mark the answer if it helped you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759582
    where i have to write ur code Nirav Lalan

  • #759585
    Hello Selvi,

    You can write this code at anywhere.
    For ex: Button Click, Checkbox checked change, textbox textchanged. Any where you can use this code in class file (.cs).

    Hope you understand.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #759808
    Hai Selvi,
    RowDataBound is the unique event to handle these types of issues. So it is better to use the same event for the GridView binding as it is quite easy and you can handle all the things well by using this event.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments