我正在用c#創建一個應用程序,silverlight。我想弄清楚如何在自動生成後對我的數據網格中的列進行重新排序。在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,estimatedSD,estimatedED,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;
}
還是什麼都沒有,但與加載的事件處理程序,我得到一個錯誤:
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)
我不知道,也許這是不可能的。我也嘗試了一個Loaded事件。依然沒有。發佈上面的代碼... – jwebster 2012-04-25 20:13:34