I am creating a an application with c#, silverlight. I am trying to figure out how to reorder the columns in my datagrid after they are autogenerated.
I tried doing something like this:
private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
switch (e.PropertyName)
{
case "taskName":
{
e.Column.DisplayIndex = 0;
break;
}
case "overallPercentComplete":
{
e.Column.DisplayIndex = 1;
break;
}
case "TDO_ID":
{
e.Column.DisplayIndex = 2;
break;
}
case "WBS_ID":
{
e.Column.DisplayIndex = 3;
break;
}
case "baseLineSD":
{
e.Column.DisplayIndex = 4;
break;
}
case "baseLineEd":
{
e.Column.DisplayIndex = 5;
break;
}
case "estimatedSD":
{
e.Column.DisplayIndex = 6;
break;
}
case "estimatedED":
{
e.Column.DisplayIndex = 7;
break;
}
case "IMS_Hours":
{
e.Column.DisplayIndex = 8;
break;
}
case "ETC_Hours":
{
e.Column.DisplayIndex = 9;
break;
}
}
}
This doesn’t exactly work correctly. For this datagrid, the order should be:
taskName, overallPercentComplete, TDO_ID, WBS_ID, baseLineSD, baseLineED, estimatedSD, estimatedED, IMS_Hours, ETC_Hours.
I thought by modifying the Column.DisplayIndex property that would set it up correctly.
However, when this code actually executes, the order is:
taskNme, baseLineEd, TDO_ID, WBS_ID, overallPercentComplete, baseLineSD, estimatedED, estimatedSD, ETC_Hours, IMS_Hours.
Any ideas? Any help would be greatly appreciated. Thanks in advance.
* EDIT *
void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
dataGrid1.Columns[0].DisplayIndex = 6;
dataGrid1.Columns[1].DisplayIndex = 7;
dataGrid1.Columns[2].DisplayIndex = 8;
dataGrid1.Columns[3].DisplayIndex = 9;
dataGrid1.Columns[4].DisplayIndex = 4;
dataGrid1.Columns[5].DisplayIndex = 5;
dataGrid1.Columns[6].DisplayIndex = 3;
dataGrid1.Columns[7].DisplayIndex = 2;
dataGrid1.Columns[8].DisplayIndex = 0;
dataGrid1.Columns[9].DisplayIndex = 1;
}
Still nothing, but with the Loaded event handler, I get an error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Error Details:
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
at camDashboard.Views.Details.dataGrid1_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)
Unfortunately this indeed seems to be impossible in Silverlight today.
Having produced a working solution using the AutoGeneratedColumns event in WPF, I have checked the Silverlight DataGrid source code with Reflector, and first of all, there is no AutoGeneratedColumns event, and second, they indeed sort the columns inside
GenerateColumns()(the location your event handler is called from) using a fixed algorithm regardless of your display index settings.I can update this placeholder with the WPF solution if it’s of any interest.