ASP.NET - Gridview get column total - Asked By paramesh nomula on 13-Feb-12 08:30 AM

 
   how to get column total in gridview


i have one gridview column like Ammount  

i want to caluculate and display sum in to footer in grid view
Venkat K replied to paramesh nomula on 13-Feb-12 09:05 AM
<asp:TemplateField HeaderText="Amount" SortExpression="Amount">
<ItemTemplate>
<asp:Literal ID="AmountLiteral" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:Literal ID="AmountTotal" runat="server" />
</FooterTemplate>
</asp:TemplateField>

In your code behind, you have a private variable on top as global variable.

private decimal total = 0;

In your onRowDataBound Method, each row, I get the amount and add it to total, when I reached the footer, I simply set to the literal of the total value.

protected void InvoicesGridView_DataBound(object sender, GridViewRowEventArgs e)
{
Invoices_Ext invoice = (Invoices_Ext)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Literal AmountLiteral = (Literal)e.Row.FindControl("AmountLiteral");
AmountLiteral.Text = String.Format("{0:C2}", invoice.InvoiceAmount);
total += invoice.InvoiceAmount.Value;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Literal AmountTotal = (Literal)e.Row.FindControl("AmountTotal");
AmountTotal.Text = String.Format("{0:C2}", total);
}
}

Thanks
dipa ahuja replied to paramesh nomula on 13-Feb-12 09:46 AM
Add the footer Template:
 
<asp:TemplateField HeaderText="Id">
  <ItemTemplate>
  <asp:Label ID="lblprice" runat="Server" Text='<%# Eval("price") %>' />
  </ItemTemplate>
  <FooterTemplate>
  <asp:Label ID="lbltotal" runat="server" />
  </FooterTemplate>
</asp:TemplateField>
 
Implement the RowDataBound
 
decimal totalPrice = 0M;
int totalItems = 0;
 
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    Label lblPrice = (Label)e.Row.FindControl("lblprice");
    decimal price = Decimal.Parse(lblPrice.Text);
    totalPrice += price;
    totalItems += 1;
  }
  if (e.Row.RowType == DataControlRowType.Footer)
  {
    Label lblTotalPrice = (Label)e.Row.FindControl("lbltotal");  
    lblTotalPrice.Text = totalPrice.ToString();  
  }
  }
 
kalpana aparnathi replied to paramesh nomula on 13-Feb-12 02:14 PM
hi,

Instated of using grid view row property retrive no of rows from the data set directly because row property changes with the paging

     
foreach (DataRow row in ds1.Tables[0].Rows)
                       
{
                            totCal
+= Convert.ToDouble(row["salary"]);
                       
}
                        lblsal
.Text = " " + totCal;

Hope will works!!!!!!!!!

Regards,
paramesh nomula replied to kalpana aparnathi on 14-Feb-12 02:31 AM
   ThanQ kalpana 
How to delete a Record in gridview - paramesh nomula replied to kalpana aparnathi on 14-Feb-12 02:32 AM
How to delete a Record in gridview 
kalpana aparnathi replied to paramesh nomula on 14-Feb-12 02:57 AM

Try this

<asp:LinkButton ID="lnkDelete" runat="server" OnClientClick='fun(<%#Eval("FileName") %>)' Text="Delete"></asp:LinkButton>

paramesh nomula replied to kalpana aparnathi on 14-Feb-12 03:16 AM
Please give me fun() code in aspx.cs page
kalpana aparnathi replied to paramesh nomula on 14-Feb-12 03:21 AM
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);
 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "DELETE FROM tblname WHERE id=@id";
cmd.Parameters.Add(
"@id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
 
cmd.Connection = con;
 
con.Open();
 
cmd.ExecuteNonQuery();
 
con.Close();
 
BindData();
 
}
paramesh nomula replied to kalpana aparnathi on 14-Feb-12 03:30 AM


this is my bind code how to delete record


DataSet

ds = new DataSet();

ds = objbll.GetEmpdetails();

grdemp.DataSource = ds;

grdemp.DataBind();

kalpana aparnathi replied to paramesh nomula on 14-Feb-12 03:41 AM
hi,
  
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
   int id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindContro("lblid")).Text);//ID
  SqlConnection connect = new SqlConnection(ConnString);
   connect.Open();
 
DataSet ds = new DataSet();
 
ds = objbll.GetEmpdetails();
  string q = "Delete from table where id=@id";
  SqlCommand comm = new SqlCommand(q, connect);
   comm.Parameters.AddWithValue("id", id);
   comm.ExecuteNonQuery();
   connect.Close();
  grdemp.EditIndex = -1;
   
  
grdemp.DataSource = ds;
  
grdemp.DataBind();
}