Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

grandtotal in end of gridview

Posted on 2009-05-20
5
Medium Priority
?
827 Views
Last Modified: 2013-12-07
i wanna put grand total in the end of gridview..i have 3 columns in my gridview and i wanna grandtotal of 2 and 3 columns...

all these three columns are coming from database..
1st column is haveing label,2nd is having textbox,3rd is having textbox.....
so i wanna gridview as..
at the end of gridview in first columns "grandtotal" and in 2nd column total of 2nd colum and 3rd column end total of 3rd column...
gridview code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        CellPadding="4" ForeColor="#333333" GridLines="None">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
                        <Columns>
                            <asp:TemplateField HeaderText="Subjects">
                                <ItemTemplate>
                                    <asp:Label ID="grd_lbl" runat="server" Text='<%# Eval("subject") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Marks Obtained">
                                <ItemTemplate>
                                    <asp:TextBox ID="grd_marks" Text='<%# Eval("marks") %>' runat="server"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Total Marks">
                                <ItemTemplate>
                                    <asp:TextBox ID="grd_total" Text='<%# Eval("outof") %>'  runat="server"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            
                        </Columns>
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#999999" />
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    </asp:GridView>
 
 
 
and cs code
 
 
SqlCommand cmd = new SqlCommand("select subject,marks,outof from marks where roll_no=@rn and c_id=@id", con);
        cmd.Parameters.AddWithValue("@rn", TextBox1.Text);
        cmd.Parameters.AddWithValue("@id", a);
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();

Open in new window

1.jpg
0
Comment
Question by:mahajan344
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 7

Assisted Solution

by:Alpha Au
Alpha Au earned 80 total points
ID: 24429668
0
 

Author Comment

by:mahajan344
ID: 24429693
i don't know VB..
i am using page language C#.. so can u please give me the code of c#
0
 
LVL 59

Assisted Solution

by:HainKurt
HainKurt earned 80 total points
ID: 24433419
1. you can find the sums first and a record to the datasource before binding it to your grid view
2. use events to sum and display (if data is relatively small, say <= 300 records





Method 1.
 
Dim dtMarks As DataTable = dtCases.Table
Dim drMarks As DataRow = dtMarks.NewRow()
 
drMarks("subjects") = "Grand Total"
drMarks("marks_obtained") = dtMarks.Compute("SUM(marks_obtained)", String.Empty).ToString
drMarks("total_marks") = dtMarks.Compute("SUM(total_marks)", String.Empty).ToString
 
dtMarks.Rows.Add(drMarks)
 
Method 2.
 
private sum_marks_obtained as integer = 0
private sum_total_marks as integer = 0 
 
Protected Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles mydatagrid.ItemDataBound
	Select Case e.Item.ItemType
		Case ListItemType.Item, ListItemType.AlternatingItem
			sum_marks_obtained += CType(e.Item.Cells(3).Text, Double)
			sum_total_marks += CType(e.Item.Cells(4).Text, Double)
		Case ListItemType.Footer
			e.Item.Cells(2).Text = "GRAND TOTAL: "
			e.Item.Cells(3).Text = sum_marks_obtained.ToString
			e.Item.Cells(4).Text = sum_total_marks.ToString
	End Select
End Sub

Open in new window

0
 

Accepted Solution

by:
mahajan344 earned 0 total points
ID: 24456970
float a1 = 0, b1 = 0, c1 = 0, d1 = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                a1 = float.Parse(((TextBox)row.FindControl("grd_marks")).Text);
                c1 = c1 + a1;
                b1 = float.Parse(((TextBox)row.FindControl("grd_total")).Text);
                d1 = d1 + b1;
            }
        }
        GridView1.FooterRow.Cells[1].Text = c1.ToString();
        GridView1.FooterRow.Cells[2].Text = d1.ToString();


i got answer...just show footer of grid..
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question