Skip to content

Instantly share code, notes, and snippets.

@craigeddy
Last active September 5, 2017 11:19
Show Gist options
  • Save craigeddy/bd1b5f1a819ccb7f439885d82f39abd1 to your computer and use it in GitHub Desktop.
Save craigeddy/bd1b5f1a819ccb7f439885d82f39abd1 to your computer and use it in GitHub Desktop.
Grouping columns programmatically in the Telerik RadGridView control for WinForms
// http://docs.telerik.com/devtools/winforms/gridview/view-definitions/column-groups-view
// Grouped columns do not wrap header text; see http://www.telerik.com/forums/grouping-column-headers
var view = new ColumnGroupsViewDefinition();
var group = new GridViewColumnGroup("Reference Data");
view.ColumnGroups.Add(group);
var groupRow = new GridViewColumnGroupRow();
group.Rows.Add(groupRow);
groupRow.Columns.Add(gridItems.Columns["colRecordType"]);
groupRow.Columns.Add(gridItems.Columns["colFundingAgency"]);
groupRow.Columns.Add(gridItems.Columns["colMechanismID"]);
groupRow.Columns.ToList().ForEach(c => c.WrapText = true);
group.IsPinned = true;
group = new GridViewColumnGroup("");
view.ColumnGroups.Add(group);
groupRow = new GridViewColumnGroupRow();
group.Rows.Add(groupRow);
groupRow.Columns.Add(gridItems.Columns["colMechanismName"]);
groupRow.Columns.Add(gridItems.Columns["colPrimePartner"]);
groupRow.Columns.Add(gridItems.Columns["colTBDMechanism"]);
groupRow.Columns.Add(gridItems.Columns["colNewMechanism"]);
groupRow.Columns.Add(gridItems.Columns["colWorkingCapitalFund"]);
groupRow.Columns.ToList().ForEach(c => c.WrapText = true);
group = new GridViewColumnGroup("EE");
view.ColumnGroups.Add(group);
groupRow = new GridViewColumnGroupRow();
group.Rows.Add(groupRow);
groupRow.Columns.Add(gridItems.Columns["colGHP_State"]);
groupRow.Columns.ToList().ForEach(c => c.WrapText = true);
group = new GridViewColumnGroup("DD");
view.ColumnGroups.Add(group);
groupRow = new GridViewColumnGroupRow();
group.Rows.Add(groupRow);
groupRow.Columns.Add(gridItems.Columns["colGHP_USAID"]);
groupRow.Columns.ToList().ForEach(c => c.WrapText = true);
gridItems.ViewDefinition = view;
@craigeddy
Copy link
Author

Note that newer versions of the RadGridView support design-time column groupings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment