title: Reording columns after column autogeneration in C#/Silverlight Program
body: <p>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.</p>
  
  <p>I tried doing something like this:</p>
  
  <pre><code>    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;
                  }
          }
      }   
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Any ideas? Any help would be greatly appreciated. Thanks in advance.</p>
  
  <p><em><strong></em>* EDIT <em>*</em></strong></p>
  
  <pre><code>  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;
      }
  </code></pre>
  
  <p>Still nothing, but with the Loaded event handler, I get an error:</p>
  
  <pre><code>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)
  </code></pre>