This is a migrated thread and some comments may be shown as answers.

Multi Column Group in RadListView

3 Answers 386 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Khuram
Top achievements
Rank 1
Khuram asked on 13 Jan 2021, 09:54 AM

Hello , I am new in Telerik WinForms.

My situation is group all items by its Product Name, while Product has its own price . e.g Product-1 has multiple values/items , similaly Product-2 has own items/values. I know in this situation Grouping concept is used i-e Group By Product, but I want to show Price of that Group along with Product Name.

For clarity i have attached a picture. I want this result in RadListView / Any other Control. Please help me along with coding.

 

Thanks

 

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Jan 2021, 11:21 AM

Hello, Khuram,

RadListView supports grouping functionality: https://docs.telerik.com/devtools/winforms/controls/listview/features/grouping 

Thus, you can organize the items in different groups according to a certain field. Then, handling the VisualItemFormatting event you can customize the title for each group: 

        private void RadForm1_Load(object sender, EventArgs e)
        { 
            this.employeesTableAdapter.Fill(this.nwindDataSet.Employees);

            this.radListView1.VisualItemFormatting += radListView1_VisualItemFormatting;
            this.radListView1.DisplayMember = "LastName";
            this.radListView1.ValueMember = "EmployeeID";
            this.radListView1.DataSource = this.employeesBindingSource;

            radListView1.EnableGrouping = true;
            radListView1.ShowGroups = true;
            GroupDescriptor groupByValue = new GroupDescriptor(new SortDescriptor[] { new SortDescriptor("TitleOfCourtesy", ListSortDirection.Descending) });
            radListView1.GroupDescriptors.Add(groupByValue);
        }

        private void radListView1_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
        {
            SimpleListViewGroupVisualItem groupItem = e.VisualItem as SimpleListViewGroupVisualItem;
            if (groupItem != null)
            {
                ListViewDataItemGroup dataGroup = groupItem.Data as ListViewDataItemGroup;
                DataRowView firstGroupItem = dataGroup.Items[0].DataBoundItem as DataRowView;
                groupItem.Text = dataGroup.Value + firstGroupItem.Row["LastName"].ToString();
            }
        }

You can also define custom groups with the exact text that you need to display: 

https://docs.telerik.com/devtools/winforms/controls/listview/features/grouping#custom-grouping 

Feel free to use this approach which suits your custom requirements best.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Khuram
Top achievements
Rank 1
answered on 13 Jan 2021, 12:09 PM
Can you please give example on my attached picture. I am using SQLite as database. 
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Jan 2021, 08:44 AM

Hello, Khuram,   

I have prepared a sample code snippet for your reference. Please refer to the below example. It works with a DataTable for simplicity of the example. It is up to the developer how the data is extracted. Let's focus on how the groups are defined and formatted: 

        public RadForm1()
        {
            InitializeComponent();
            this.radListView1.VisualItemFormatting += RadListView1_VisualItemFormatting;
            DataTable dt = new DataTable();
            dt.Columns.Add("Quantity",typeof(string));
            dt.Columns.Add("ItemName", typeof(string));
            dt.Columns.Add("Price", typeof(int));
            dt.Columns.Add("ProductName", typeof(string));

            dt.Rows.Add("2x","Item1",15,"Product1");
            dt.Rows.Add("1x", "Item2", 10, "Product1");
            dt.Rows.Add("4x", "Item3", 10, "Product1");
            dt.Rows.Add("1x", "Item4", 15, "Product2");
            dt.Rows.Add("1x", "Item5", 10, "Product2");

            this.radListView1.DataSource = dt;
            this.radListView1.DisplayMember = "ItemName";
            radListView1.EnableGrouping = true;
            radListView1.ShowGroups = true;
            GroupDescriptor groupByValue = new GroupDescriptor(new SortDescriptor[] { new SortDescriptor("ProductName", ListSortDirection.Ascending) });
            radListView1.GroupDescriptors.Add(groupByValue);

        }

        private void RadListView1_VisualItemFormatting(object sender, Telerik.WinControls.UI.ListViewVisualItemEventArgs e)
        {
            SimpleListViewGroupVisualItem groupItem = e.VisualItem as SimpleListViewGroupVisualItem;
            if (groupItem != null)
            {
                ListViewDataItemGroup dataGroup = groupItem.Data as ListViewDataItemGroup;

                int totalPrice = 0;
                foreach (ListViewDataItem item in dataGroup.Items)
                {
                    DataRowView dataBoundItem = item.DataBoundItem as DataRowView;
                    totalPrice += (int)dataBoundItem.Row["Price"];
                }
                groupItem.Text = dataGroup.Value +", Price "+ totalPrice;

            }
        }

 

I hope that you will find this approach suitable for your scenario.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
ListView
Asked by
Khuram
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Khuram
Top achievements
Rank 1
Share this question
or