Solved

grandtotal in end of gridview

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

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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 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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

734 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