Solved

grandtotal in end of gridview

Posted on 2009-05-20
5
819 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
  • 2
5 Comments
 
LVL 7

Assisted Solution

by:alphaau
alphaau earned 20 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 51

Assisted Solution

by:HainKurt
HainKurt earned 20 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now