0

I have written the code that pull data from SQL database and show in listview1 within group. I want find the grand total of each group and show it on the last row of the group.

I have attached the screenshot of what i am looking for.

enter image description here

Code to group SQL data into list view

// This flag will tell us if proper group already exists
        bool group_exists = false;

        con = new SqlConnection(cs);
        con.Open();

        cmd = new SqlCommand("Select * from subhead where id="+hid, con);
        SqlDataReader Reader1 = cmd.ExecuteReader();
        List<Sample> result1 = new List<Sample>();
        while (Reader1.Read())
        {
            var daybookHeadobj = new Sample
            {
                headorgID = Reader1.GetInt32(0),
                headorgName = Reader1.GetString(2),

            };

            //daybookHeadobj.total = daybookHeadobj.TransferCredit + daybookHeadobj.CashCredit;

            result1.Add(daybookHeadobj);



        // Check each group if it fits to the item
        foreach (ListViewGroup group in this.listView1.Groups)
        {
            // Compare group's header to selected subitem's text
            if (group.Header == daybookHeadobj.headorgName)
            {
                // Add item to the group.
                // Alternative is: group.Items.Add(item);
                item.Group = group;
                group_exists = true;
                break;

            }

        }

        // Create new group if no proper group was found
        if (!group_exists)
        {
            // Create group and specify its header by
            // getting selected subitem's text
            ListViewGroup group = new ListViewGroup(daybookHeadobj.headorgName);
            // We need to add the group to the ListView first
            this.listView1.Groups.Add(group);
            item.Group = group;

        }

        }

code that pull data from database

listView1.GridLines = true;
        listView1.View = View.Details;

        //Add Column Header
        listView1.Columns.Add("Date", 150);
        listView1.Columns.Add("Receipt No", 150);
        listView1.Columns.Add("Details", 150);
        listView1.Columns.Add("Cash Credit", 150);
        listView1.Columns.Add("Transfer Credit Adjustment", 150);
        listView1.Columns.Add("Total", 150);

        con = new SqlConnection(cs);
        con.Open();

        //cmd = new SqlCommand("Select headid from daybook_credit GROUP BY headid", con);


        // Chnage sql query and table name
        cmd = new SqlCommand("Select * from daybook_credit ORDER BY headid ASC", con);
        SqlDataReader Reader = cmd.ExecuteReader();

        listView1.Items.Clear();

        List<Sample> result = new List<Sample>();
        while (Reader.Read())
        {
            var daybookDBobj = new Sample
            {
                ID = Reader.GetInt32(0),
                HeadID = Reader.GetInt32(1),
                Date = Reader.GetString(2),
                RecieptNo = Reader.GetInt32(3),
                details = Reader.GetString(4),
                CashCredit = Reader.GetInt32(5),
                TransferCredit = Reader.GetInt32(6),
                total = Reader.GetInt32(5) +  Reader.GetInt32(6)
            };
            result.Add(daybookDBobj);
            // ListViewItem has a useful constructor that
            // allows us to add colection of subitems
            // in form of a string array
            ListViewItem item = new ListViewItem(new string[] {
                    daybookDBobj.Date.ToString(),daybookDBobj.RecieptNo.ToString(),daybookDBobj.details.ToString(),daybookDBobj.CashCredit.ToString(),daybookDBobj.TransferCredit.ToString(),daybookDBobj.total.ToString()}, daybookDBobj.HeadID.ToString());

            // Call our item-grouping function,
            // which will properly group the item
            GroupItem(item, daybookDBobj.HeadID.ToString());

            // Finally, add the item to the ListView
            this.listView1.Items.Add(item);


        }


        Reader.Close();
        con.Close();
  • Could you show us what you have tried? –  Dec 09 '16 at 09:44
  • Which object contains a list of totals? You could use LINQ and use `.Sum()` to sum up all numbers in the array. [This](http://stackoverflow.com/questions/2419343/how-to-sum-up-an-array-of-integers-in-c-sharp) question gives an example. –  Dec 09 '16 at 09:51
  • how to find each items in a group? – Virtual Hosting Dec 09 '16 at 09:57
  • You can store the totals in a list or array and sum them up. I'm not sure what the structure of your class is, I can help more if you'd show that. –  Dec 09 '16 at 09:58
  • @Bas I have the code that pull data from database – Virtual Hosting Dec 09 '16 at 10:02

0 Answers0