• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

grandtotal in end of gridview

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
mahajan344
Asked:
mahajan344
  • 2
3 Solutions
 
mahajan344Author Commented:
i don't know VB..
i am using page language C#.. so can u please give me the code of c#
0
 
HainKurtSr. System AnalystCommented:
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
 
mahajan344Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now