Solved

grandtotal in end of gridview

Posted on 2009-05-20
5
820 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 47
Receiving a string from a WebService Push 21 36
How do I call MySQL Stored Procedure from oracle using HS link ? 5 25
Reset asp.net login password 4 28
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

21 Experts available now in Live!

Get 1:1 Help Now