Grouping DataGridView with bound data

0
0

Hello,

I’m trying to group a DataGridView  with bound data. It seems the control does not support this use case.  Please correct if  I’m wrong.

The API for grouping DGV is a little bit technical. I think the API should be simple like setting a group level in column properties. So at runtime, we can provide our user to select what column they wan to group. or better, drag a column to grouping area.

Exception :

Rows cannot be programmatically added to the DataGridView’s rows collection when the control is data-bound.

Here is my minimum code:

——————————————————
using System;
using System.Collections.Generic;
using Wisej.Web;

namespace WisejWebDesktopApplication2
{
public partial class Window1 : Form
{
public Window1()
{
InitializeComponent();

dataGridView1.AutoGenerateColumns = true;

}

private void Window1_Load(object sender, EventArgs e)
{
BindData();

this.dataGridView1.AddSummaryRows(SummaryType.Count, SummaryRowPosition.Above, this.dataGridView1.Columns[2], this.dataGridView1.Columns[2]);

}
private void BindData()
{
var data = new List<EmplInfo>();
data.Add(new EmplInfo() { EmplCode = “E001”, EmplName = “Employee 001”, EmplGroup = “DirectHired” });
data.Add(new EmplInfo() { EmplCode = “E002”, EmplName = “Employee 002”, EmplGroup = “DirectHired” });
data.Add(new EmplInfo() { EmplCode = “E003”, EmplName = “Employee 003”, EmplGroup = “DirectHired” });
data.Add(new EmplInfo() { EmplCode = “E004”, EmplName = “Employee 004”, EmplGroup = “Secondaire” });
data.Add(new EmplInfo() { EmplCode = “E005”, EmplName = “Employee 005”, EmplGroup = “Secondaire” });

this.dataGridView1.DataSource = data;
}
}

 

internal class EmplInfo
{

public string EmplCode { get; set; } = “”;
public string EmplName { get; set; } = “”;

public string EmplGroup { get; set; } = “”;

}
}

 

 


*Edited by Frank*

  • You must to post comments
0
0

Hi Tung,

in order to use the full grouping functionality in a DataGridView you can call one of the following functions

Fill
Append

Best regards
Frank

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.