.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

Sorting a specific DataGridColumn in wpf DataGrid

Posted By:      Posted Date: September 09, 2010    Points: 0   Category :WPF
Hi! I have a WPF DataGrid that has several DataGridColumns, one of these columns is bound to a Property that is an abstract class. Like this:    public class Cat { public AbstractCatType Type { get; set; } } public class AbstractCatTypeToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is NiceCatType) { return "Nice"; } if (value is BadCatType) { return "Bad"; } return "unknown"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new System.NotImplementedException(); } } When i click the column header to sort this column I get (not very suprising) an exception System.InvalidOperationException was unhandled Message="Failed to compare two elements in the array." I would like the column to get sorted on the string values it contains (Bad, Nice) but how can I do that? I would prefer to specify a Comparer class for that column or if I could make it forget about the property that it is bound to and sort the strings written in the column. I would prefer not to have to create specific comparers for all of my columns and I have no other SortMemberPath to redirect the sorti

View Complete Post

More Related Resource Links

Sorting by Calculated Templatefield in Datagrid


I have tried several different approaches .


SOMEONE PLEASE sow me how to sort on template field with a calculated value.

Basically i have half the data i need in the DB and the other half comes from textbox1.text

I take the text in textbox1 and pass it along with a value from the DB (Orig) to a function that returns miles.

                    <asp:Label ID="Label1" runat="server" Text='<%# miles(Eval("Orig"), textbox1.text) %>'>

Whit the above method everything displays correctly but i cannot sort on that column .


I also tried using the miles() function in my select statement for my datasource but that was a nightmare and couldn't get that to work i either got "undeclared function "miles()" or when i fixed that i got "orig" not declared.

Sorry code is so jumbled , entire code is below:

<%@ Page Title="" Language="VB" MasterPageFile="~/Secure/MasterPage.master" %>

<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
    ' I

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.


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

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

Sorting and Paging in DataGrid which is inside asp:wizard



Would it be possible to do Sorting and Paging in a DataGrid, My dataGrid is place inside an asp:wizard which is inside an Update Panel.

<asp:UpdatePanel ID="upEditusr" runat="server">
<asp:Wizard ID="Wizard1" runat="server" CssClass="wizUsrAccess" displaycancelbutton="True" 
              oncancelbuttonclick="OnCancelButtonClick" onfinishbuttonclick="OnFinishButtonClick" OnNextButtonClick="OnNextButtonClick" ActiveStepIndex="0" BorderWidth="1px" DisplaySideBar="false">
               <asp:Label ID="SideBarInfo" OnPreRender="InitSideBarLbl" runat="server" /> 
               <asp:DataList ID="SideBarList" OnPreRender="LoadSideBar" RepeatLayout="Flow" Style="display:none;" runat="server"> 
                       <asp:LinkButton ID="SideBarButton" Visible="false" runat="server"></asp:LinkButton> 
                   <SelectedItemStyle Font-Bold="True"  />            

Datagrid: How to select a specific row by program?


How to select a specific row in the Datagrid (in WPF) programmatically? (like datagridview1.Rows(index).Selected = True in Windows form)?

I wants to select first row programmatically by defualt after it populating all the records. I treid DataGrid1.Selectedcell and all similar methods, but not able to get what I need. It looks that Data grid in WPF does not have anything related to Row while DataGrid is in Windows form have many things related to Row



Dreaming a world without any war in anywhere

Run javascript before sorting or paging the datagrid


Here is what I am trying to do;

I want it so that when the user clicks on the datagrid column headers to sort the records, or when they go to page the datagrid, that a confirmation pop-up appears first, confirming that their changes are going to be lost (unless they click "Cancel").

I have the confirmation pop-up scripted in Javascript and is added to the controls on the page using

onClientClick="return ConfirmDiscardChanges();"

How do I attach this to a datagrid header and to the pager?

Do the columns need to be Template columns and the header text being hyperlink?  If so, how do I set it up so it will, if the user clicks "OK" in the confirmation pop-up,  still sort?


AJAX GridView Paging and Sorting using C# in ASP.Net

To enable the GridView Paging and Sorting to update the GridView Data without refreshing the ASP.Net web page AJAX Extension controls are used. UpdatePanel is used to hold the GridView control that triggers the events for the controls placed inside it. By Default UpdatePanel triggers the asynchronous postbacks for its child controls.

Sorting Gridview using Jquery with ASP.NET

I want to show how to sorting a gridview using a Jquery in ASP.NET, it is a flexible client-side table sorting. We have known that Jquery is a new kind of Javascript Library. You can find detail of Jquery in this site http://jquery.com/ there are lot of example with documentation and also allow free download a latest version.

How to format and update GridView and DataGrid rows using JQuery

The behavior described in this question is as expected. When you set text of a cell in grid, it directly affects HTML that is going to be rendered. When you set text value of a cell, it means that you are setting innerText of the cell. The column that GridView creates for command fields (Edit, Delete and Select) are a (anchor) or button elements. So you can see what will happen if you set text value in that cell. It will wipe out those link or button controls and replace them with simple text string.

Sorting Gridview using Jquery with ASP.NET

used table sorter plug-in which has written by Christian Bach can found http://tablesorter.com/docs/ . It is really cool plug-in and user can allow to download and customization as their wish.

I test this example in Visual Studio 2005. Some of other requirement needed is download a latest version of Jquery from official website and table sorter plug-in which can found as I mention above website.

Use Jquery with repeater, gridview, datagrid

Before we talk about jquery inside any itemtemplate (repeater, gridview, datagrid), we need to understand the basic use of jquery functions

ListBox tricky to set in a DataGrid/GridView

Listboxes are very tricky to set selected items in grid - there is no real nice way to do it. It would have been great if Microsoft included a property that accepts an array or comma separated values - or anything! But they only allow each item in the list to be selected individually. The way to do this in a grid is a bit messy, but it is fairly simple to implement. When a row is created (as per the event), select the items of the listbox as per the datagrid source data:

GridView Sorting/Paging : 'REAL' AJAX with Asp.Net Series

This is a continuation of my 'REAL' AJAX with Asp.Net (not Asp.Net AJAX) series posts for those of us trying to stop relying on Asp.Net 'AJAX'.

Ok, nothing too dramatic to start things off, we are going to make some tabled data sortable and pagable all without using a bit of postback. Not only that, but it is going to be incredibly easy!

How to highlight datagrid or GridView row on mouse over?

This article will show how you can Ajax with GridView to display popup messages when mouse moves over certain column. In the demo project I have added first column as an image column with a help icon in it. This kind of implements a feature that if you want more information about the row you can move mouse over this icon, a asynchronous request is sent to server for data about that row and when call returns the returned message is show as a popup message.

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.

Using the WPF Toolkit DataGrid

WPF comes with a large number of built in controls, but from the beginning it has lacked something that many application developers find extremely important - a DataGrid. You can use the ListView to create something approximating a DataGrid

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
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