我正在使用 C# Silverlight 创建一个应用程序。我试图弄清楚如何在我的数据网格中的列自动生成后重新排序。
我试着做这样的事情:
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;
}
}
}
这并不完全正确。对于此数据网格,顺序应为:taskName、overallPercentComplete、TDO_ID、WBS_ID、baseLineSD、baseLineED、estimateSD、estimateED、IMS_Hours、ETC_Hours。
我想通过修改 Column.DisplayIndex 属性来正确设置它。但是,这段代码实际执行时,顺序是:taskNme、baseLineEd、TDO_ID、WBS_ID、overallPercentComplete、baseLineSD、estimatedED、estimatedSD、ETC_Hours、IMS_Hours。
有任何想法吗?任何帮助将不胜感激。提前致谢。
* 编辑*
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;
}
仍然没有,但是使用 Loaded 事件处理程序,我得到一个错误:
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)