Thursday, April 8, 2010

Preserve the previous sort order for WPF Datagrid

When you reload the WPF Datagrid's ItemsSource, the previous SortOrder is not retained. Here is a simple way you can preserve the sort order.

1. Before you reload the ItemsSource, save the sort column, sort direction as follows:

string dgSortDescription = null;
ListSortDirection? dgSortDirection = null;
int columnIndex = 0;
System.Collections.ObjectModel.ObservableCollection itemsColl= null;

foreach (DataGridColumn column in myDatagrid.Columns)
{

columnIndex++;

if (column.SortDirection != null)
{

dgSortDirection = column.SortDirection;

dgSortDescription = column.SortMemberPath;

break;
}

}



2. Now reload your collection with new data


itemsColl = new myNewcollection();

this.myDatagrid.ItemsSource = itemsColl;



3. Attach the saved sort description back as follows:



if(!string.IsNullOrEmpty(dgSortDescription) && dgSortDirection != null)

{

SortDescription s = new SortDescription(dgSortDescription, dgSortDirection.Value);

CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(myDatagrid.ItemsSource);
view.SortDescriptions.Add(s);

myDatagrid.Columns[columnIndex - 1].SortDirection = dgSortDirection;

}

Wednesday, April 7, 2010

Single Instance WPF Application

In this article, I will share a basic simple ways to create a single instance application. Though there are multiple ways to do it, this is what I did in one my recent projects. You may get a stupid answer - "This is simple and fast".

Method 1:

Add the following code to your startup window (which is usually App.xaml.cs) after InitializeComponent().

Process thisProcess = Process.GetCurrentProcess();

if(Process.GetProcessesByName(thisProcess.ProcessName).Length >1 )
{

// If ther is more than one, than it is already running.

MessageBox.Show("Another instance of your app is already running.");

Application.Current.Shutdown();

return;

}


Tuesday, April 6, 2010

My Interests

I'm a full stack .NET developer. If you have any questions, you can send me an email at raghu dot muttana at g mail dot com. Wrote on 4/6/2010: Currentlly I'm working in WPF/Silverlight technologies. I will be posting some of the challenges I face in my daily development work, some quick solutions, tips and tricks to get away with development roadblocks. At times, content may be copied from other blogs assuming "knowledge is free". I gladly accept all your comments, feedback on the technical topics you see in my blog.

Friday, April 2, 2010

My first blog

I am new to blogging. I created this blog to make myself more comfortable in writing.

Combinable Enumerations         [Flags]         public enum SomeFlags         {             NoColor = 0,             Red = 1,        ...