Hierarchical Data Grid View Sort

C# DataGridView Sorting and Filtering The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. A DataView provides a means to filter and sort data within a DataTable.

  1. Hierarchical Data Grid View Sorting
I want to display child rows in the datagrid after the + sign is selected (like grouping & outlining in excel). This is automatic in infragistic grids but when i did in asp (no more infragistics) it doesnt work. here is my code (very simple stuff)
<%@ Page Language='C#' AutoEventWireup='true' CodeFile='Default.aspx.cs' Inherits='_Default' %>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat='server'>
<title></title>
</head>
<body>
<form runat='server'>
<div>
<asp:GridView runat='server'>
</asp:GridView>
</div>
</form>
</body>
</html>
DataSet ds = new DataSet();
ds.Tables.Add(myDataAccess.ExecuteSelectCommand('uspCategories'));
ds.Tables[0].TableName = 'tblCategories';
ds.Tables.Add(myDataAccess.ExecuteSelectCommand('uspProducts'));

Hierarchical Data Grid View Sorting

ds.Tables[1].TableName = 'tblProducts';
DataRelation rel = new DataRelation('relation', ds.Tables[0].Columns['CategoryID'], ds.Tables[1].Columns['CategoryID']);
ds.Relations.Add(rel);
GridView1.DataSource = ds;
GridView1.DataBind();


The attached project shows how to display data which is both tabular and hierarchical in nature in the Silverlight (or WPF) DataGrid. Employee list is an example of such data, where each employee is described using the same set of data column but there is also a reporting relationship between employees and their managers (also employees). The implementation uses DataGrid's functionality to load and display individual rows and columns. It then adds some special UI and functionality to handle the TreeView-like functionality for expanding and collapsing hierarchical relationships between rows. The latter is accomplished through the following:


Hierarchical

1.Data is exposed to the UI through a set of presenters, which describe both the tabular and hierarchical nature of the data model (GridDef, RowDef and ColumnDef classes).


2.The first column in the DataGrid is defined as DataGridTemplateColumn.


3.The column template consists of three components, each databound to a property of the row definition (RowDef):


1.A checkbox. Its IsChecked property is bound to the IsExpanded property of the row definition. Its margin property is bound to the row’s HierarchyLevel property (through a converter which converts sequential level to a Thickness structure).


2.TextBlock or other element bound to the tabular valu of the first column (e.g. employee name).


4.The checkbox in the above template is re-templatized so that instead of showing the usual rectangle, swish and other content, it only shows a triangle pointing down or to the side or nothing at all depending on whether the checkbox’s IsChecked property is true, false or null.

While secretly wishing they were in their place. Season First impressions from the Pilot show 'Flashpoint' to be a hybrid of special forces, hostage situations, lots of weapons and character study. It's a tough job.


5.The implementation of the IsExpanded property sets or resets row’s IsVisible property (a local property, not a UI property).


6.The GridDef object (main presenter) has a Display method, which returns an IEnumerable. It iterates over the data and returns (though the yield statement) currently visible rows.


In a future blog I will suggest an implementation to handle sorting functionality.