.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

DataGrid - how to make Column sorting dynamic, to cater for when bound data changes?

Posted By:      Posted Date: October 03, 2010    Points: 0   Category :WPF
I'm using a DataGrid in a VS2010 WPF C# project.  I have bound the DataGrid to an ObservableCollection<SummaryItem>.  When you click on a column heading it sorts the data at that point in time.

Question - How would I arrange such that the sorting in the DataGrid is dynamic, so that when data changes (within the ObservableCollection<SummaryItem>) the sorting keeps working.

Notes: Binding approach is via DataGrid

      private ObservableCollection<SummaryItem> _summaryData = new ObservableCollection<SummaryItem>();
      SummaryDataGrid.ItemsSource = _summaryData;
      SummaryDataGrid.AutoGeneratingColumn += (s, e) =>
        //if (e.Column.Header.ToString() == "ProcessName")
        //  e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
        e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
  public class SummaryItem : INotifyPropertyChanged
    public event PropertyChangedEvent

View Complete Post

More Related Resource Links

Set column width of data bound WPF dataGrid


Hello all,


I successfully created dataGrid that is bound to a class, so it automatically generates 4 columns.

The problem is that now I can not set the columns width, and there is always a 5th column which has no data, and fills most of the space.

How can i hide this 5th column, and adjust the width of the other ones?




How to convert the datagrid bound column item itself link to another detail page without using hyper

Dear experts, I would like to make the datagrid bound column to redirecting to the details page . For example , when my mouse over certain row of bound column in datagrid , it will change the colour to blue . Also , I can click anywhere of the row and the will redirect to the detail page . Can any expert help me on that ? Notes : I do not want to use hyperlink column and do not want to fix the text for every row. I want to use datafield, CommandName and CommandArguement. Thanks & Rgds

Datagrid column Sorting generating error


I have a datagrid column whose column values are databinded. I have used DataGridTemplateColumn and i need to use the sorting in this column.

<my:DataGridTemplateColumn SortMemberPath="FileName" Header="Name" IsReadOnly="True" MinWidth="150">

It works and sort the the data but when i edit the data after sorting, i need to re-generate the columns data.

FileListingGrid.ItemsSource = listFiles1;

But this generates "'Sorting' is not allowed during an AddNew or EditItem transaction. "

It works file when column data is not sorted but whenever i sort the data and have to re-generate the column data, it throws the following error.


How to Format Data inside a Bound Column of Gridview?



I have a bound column.

<asp:BoundField DataField="WorkPreference"  HeaderText="WorkPreference"
                    <HeaderStyle HorizontalAlign="Left" />

dr["WorkPreference"] = sa.WorkPreference ( This is how I bind the data  to a DataRow)

The example of the data which gets bound to this column is like the following:

Communications and Marketing Officers; Environmental Assessors; Geologists and Geosciences; Geospatial/Spatial Information Officers;

What I want is that the data should be presented to the user like the following:

Communications and Marketing Officers

Environmental Assessors

Geologists and Geosciences

Geospatial/Spatial Information Officers

In short: I basicaly want to put a break where there is a colon and then bind the data to the gridview.

It looks like simple but if you can let me know. Thanks.

justify column headings in dynamic data


is there a way i can change the justification of the automatically generated grid in list view so that some of the column headings are left-aligned?




WPF Datagrid Scrolling Column Headers Without Data



I have a Datagrid with enough columns that some will need to be scrolled to to see them.  The problem I am having is if the filter returns no rows, the horizontal scroll bar is greyed out and you can't scroll over to see all the column name.  I'm hoping this is an easy solution, and i'm just missing something obvious as this is my first WPF application.

Thank you for your help in advance.


Adam - Please remember to mark the answers.

Data Points: Composing WPF DataGrid Column Templates for a Better User Experience


Julie Lerman struggled for days with some vexing user experience problems related to the WPF DataGrid, so she's saving you the bother by explaining how she solved them.

Julie Lerman

MSDN Magazine April 2011

Dynamic Data Templates in GridView ASP.NET 3.5

At a high level, Dynamic Data Templates can be seen as a system to very quickly build up a User Interface linked with the relevant data model, immediately enabling you to perform CRUD operations (Create, Read, Update, Delete) on the database using the specified data source. They provide a powerful method for building up these kinds of applications, and I'm going to focus on an overview of what Dynamic Data Templates actually are, and a few ways of customizing them to suit industry needs.

ASP.NET 4.0 Dynamic Data and Many to Many Entity Framework Entities

I did not play much with Dynamic Data controls in VS2008 and just made a cool discovery in VS2010 Beta 2. This may not even be new, but as I'm sitting 30,000+ feet over the Atlantic ocean, I don't have access to VS2008 at the moment to check.

Multiple Column Dropdownlist for the ASP.NET DataGrid

Based on my previous control "Multiple Column DropDownList for ASP.NET", I received many emails asking for the same control to be used in the DataGrid for web applications. Here we go.. This control can be used as the regular MS DropDownList in the DataGrid and also as a regular dropdownlist. It has all the properties, like DataTextField, DataValueField, DataSource, SelectedIndex etc. The download file contains the samples both in VB.NET and C#. In this sample, I have used the Northwind database of SQL Server.

Sorting Data in a GridView Web Server Control

The GridView control provides built-in sorting functionality without requiring any coding. You can further customize the sort functionality of the GridView control by using custom SortExpression property values for columns as well as by using the Sorting and Sorted events



Dear gentlemen and ladies of the Microsoft Dynamic Data Team,

Mr. Steve Naughton says (and I hope so) that there might be chances that you read the posts published in this forum: I really hope so.

If that were the case then I am formally requesting to you in providing a "real answer" to my question:

I want to customize a dynamic data page (the details one) so I can have access to the data it has been already gathered from the end user UI (details.aspx) and use it to pass this same data to the following controls/pages that I am going to display next time to the end user.

For instance:

I have a screen with different types of data in it: texts, numbers, dates, check boxes, radio buttons and dropdownlists that has been filled with data by the end user. I want now to reset all fields exept the dropdownlist of which I'd like to keep the previously chosen values(the very same thing you do between dropdownlist of List.aspx ad Details.aspx). Even with FoxPro I could do that because data and controls were available there. It was as simple as setting carrying on to true.


I have found my

Routing in dynamic data applications


Im using ASP.Net 4. I tried to modified the code according to following video tutorial. http://www.asp.net/aspnet-in-net-35-sp1/videos/how-to-enable-table-specific-routing-in-dynamic-data-applications

But Product page directs only to ListDetails.aspx page.

This is my code :

<%@ Application Language="C#" %>
<%@ Import Namespace="System.ComponentModel.DataAnnotations" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.DynamicData" %>

<script RunAt="server">
    private static MetaModel s_defaultModel = new MetaModel();
    public static MetaModel DefaultModel {
        get {
            return s_defaultModel;

    public static void RegisterRoutes(RouteCollection routes) {

        DefaultModel.RegisterContext(typeof(NorthwindDataContext), new ContextConfiguration() { ScaffoldAllTables = true });


About Dynamic Data in Visual Studio 2010 / .NET 4



Does anybody here have any pointers to screencasts, demos, webcasts for the latest enhancements in Dynamic Data as listed in above link? I'm repeating them here for ready reference :


  • EnableDynamicData method which enables Dynamic Data features in existing ASP.NET applications and DataSource controls
  • A new QueryExtender control, contained in the sample projects to simplify common data filtering operations. It supports a rich ASP.NET declarative query syntax that makes it easy to do things like search data for text or have filters based on ranges
  • A new Dynamic Data filter model that enables the developers to apply templates to pages just like field templates (including user defined filters)
  • New field templates of Email, URL and Enum columns
  • Support for inheritance in Entity Framework and Linq to SQL
  • Support for many to many relationships in Entity Framework
  • New Entity Templates which allow fine control over how an object is displayed and edited
  • DynamicHyperLink control for building links to tables and actions
  • Declarative support for D

Rendering data with HTML tags in the DD Gridview for a selected column


I am having trouble finding out where and how to HTML Encode a cell's data on the Dynamic Data (v4.0) gridview of List.aspx.  As a simple case, suppose I have formatted cell data that is A<br/>B in the DB.    Obviously, I want A stacked on B in the cell.

It seems gridView1.HtmlEncode = true has gone away.

So maybe I'll try to catch it on the RowDataBound event:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)

            if (e.Row.RowType == DataControlRowType.DataRow)
                // Html Encode the cells


but this event never fires?

Has anyone figured out how to properly render HTML tags data in the List.aspx's GridView1? 

If I figure this out, then I can add a MetaAttribute called something like [EncodeAsHtml(true)] and be on my way.  Thanks!

How to sell Dynamic Data to the bosses


I have a project which has gone from Excel report to a SharePoint Custom List and now they have tried to make it an application growing legs with additional list hinging off it and no real way to clean up data from one when it deleted from another etc..(unless we add handlers etc which we don't want to do).

It was envisiaged as a simple one page list to generate some reports off of but it more a data driven application they want now.

I am looking into a way after stating this to my boss and am intrested in the quickest most flexible way to achieve this and to sell it to him.

I am more a .net person but also know plsql and most of the team I work with are Oracle specialists.

We currently have at are disposile .net, php and oracle products (portal, plsql, forms and oracle application express.)

We are at a transition stage trying to move away from Oracle Forms and I believe that .net is a better fit that php for production and readability of code.

So I am considering placing Dynamic data on the table for this project as it all data driven with data being inputed and manipulated and then reports being generated (either via code or we could hitch in a reporting tool like mssql or discover.)  The main issue will be the data will be in Oracle so if Dynamic data can handle this then it be perfect as

Dynamic Data, Domain datasource, Poco and lookups


Mr. Edward,

Excuse my question but: it is necessary to involve in our projects something more than just Dynamic Data or should we make use of all other features available in VS2010 (which will make assume taht they had been left out of the Dynamic Data framework) like: wef, poco, azure, wcf, cloud, mvc, silverllight, ajax, odata, agile, workflow, etc.?

Sorry my ignorance, I'm just transcribing names as they come to my mind and of which I don't know anything about


Carlos Porras (El Salvador)

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend