locked
How to make object of individual column for a data table and calculate the sum of datatable. RRS feed

  • Question

  • User1649939557 posted

    Can anyone please tell me how to create a object of individual column after data is binded to datatable. Also how to calculate the sum of individual column.

    For ex:

    I have a grid that looks something like this.

    Name            July        Aug        Total

    xyz                52            -              52

    abc                34           10            44

    Now what i want to do is make separate object for july and for august, calculate the sum of july column as well as august column so that my grid looks something like this:

    Name            July(total=86)        Aug(total=10)        Total

    xyz                52                               -                           52

    abc                34                              10                         44

    "total" is a label which is in my headertemplate.

    Any suggestions please?

    Wednesday, October 3, 2018 8:17 AM

All replies

  • User475983607 posted

    As stated in your other thread with the same subject, it is far easier to do calculations outside a UI control and in an environment designed to handle calculations.

    Show the SQL that generates the result set and/or the objects in use.

    Wednesday, October 3, 2018 1:40 PM
  • User1649939557 posted

    Hi mgebhard,

    Thank you for replying on both of my threads. As you suggested earlier to sum up monthly data in SQL script, I did that and now i have month total in my script. How can i show it in my UI now? 

    Actually this is not my code that's why i'm facing problem.

    Wednesday, October 3, 2018 3:14 PM
  • User475983607 posted

    Hi mgebhard,

    Thank you for replying on both of my threads. As you suggested earlier to sum up monthly data in SQL script, I did that and now i have month total in my script. How can i show it in my UI now? 

    Actually this is not my code that's why i'm facing problem.

    If you have the SUM then just assign the value to the server control. 

    If the server control is in another control then use the FindControl() method to drill into the control tree.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.control.findcontrol?view=netframework-4.7.2

    Wednesday, October 3, 2018 5:06 PM
  • User1649939557 posted

    Hi mgebhard,

    Can you show me with an example please.

    Wednesday, October 3, 2018 5:14 PM
  • User475983607 posted

    Can you show me with an example please.

    An example other than the example in the link?

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField FooterText="MyFooter" HeaderText="MyHeader">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <HeaderTemplate>
                        <asp:Label ID="HeaderLabel" runat="server" Text="Label"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
                List<string> items = new List<string> { "one", "two", "three" };
    
                Label l = (Label)GridView1.HeaderRow.FindControl("HeaderLabel");
                l.Text = "Count " + items.Count();

    Wednesday, October 3, 2018 5:34 PM
  • User839733648 posted

    Hi AdityaPradhan,

    According to your description, I suggest that you could calculate the row totals in RowDataBound event and calculate the column totals in DataBound event.

    I've made a sample on my side and for more details, you could refer to the code below.

    .aspx

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound" >
                    <RowStyle HorizontalAlign="Center" />
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150">
                            <ItemStyle Width="150px"></ItemStyle>
                        </asp:BoundField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:Label ID="JulyTotal" runat="server"></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("July") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:Label ID="AugTotal" runat="server"></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Aug") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Total">
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server"></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="150px" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    

    code-behind.

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Name"), new DataColumn("July"), new DataColumn("Aug") });
                    dt.Rows.Add("xyz", "52", "0");
                    dt.Rows.Add("abc", "34", "10");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Label july = e.Row.FindControl("Label1") as Label;
                    Label aug = e.Row.FindControl("Label2") as Label;
                    Label total = e.Row.FindControl("Label3") as Label;
                    int totalvalue = Convert.ToInt32(july.Text) + Convert.ToInt32(aug.Text);
                    total.Text = totalvalue.ToString();                 
                }
            }
    
            protected void GridView1_DataBound(object sender, EventArgs e)
            {
                int JulyAmount = 0;
                int AugAmount = 0;
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    string july = ((Label)GridView1.Rows[i].FindControl("Label1")).Text;
                    string aug = ((Label)GridView1.Rows[i].FindControl("Label2")).Text;
                    JulyAmount += Convert.ToInt32(july);
                    AugAmount += Convert.ToInt32(aug);
    
                }
                Label JulyTotal = (Label)GridView1.HeaderRow.FindControl("JulyTotal");
                Label AugTotal = (Label)GridView1.HeaderRow.FindControl("AugTotal");
                JulyTotal.Text = "July(total=" + JulyAmount.ToString() + ")";
                AugTotal.Text = "Aug(total=" + AugAmount.ToString() + ")";
            }

    result:

    Best Regards,

    Jenifer

    Tuesday, October 9, 2018 4:25 AM