Save and restore column order of DataGridView

private void DataGridView1_DataBindingComplete(object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
{
 Collections.ArrayList ColumnDisplayOrder = new Collections.ArrayList();
 //ColumnsOrder setting must be of type mscorlib.System.Collections.ArrayList
 ColumnDisplayOrder = My.Settings.ColumnDisplayOrder;
 if (ColumnDisplayOrder != null) {
  if (ColumnDisplayOrder.Count > 0) {
   for (int I = 0; I <= ColumnDisplayOrder.Count - 1; I++) {
    DataGridView1.Columns.Item(I).DisplayIndex = ColumnDisplayOrder(I);
   }
  }
 }
}

private void DataGridView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
 Collections.ArrayList ColumnDisplayOrder = new Collections.ArrayList();
 for (int I = 0; I <= DataGridView1.Columns.Count - 1; I++) {
  ColumnDisplayOrder.Add(DataGridView1.Columns.Item(I).DisplayIndex);
 }
 //ColumnsOrder setting must be of type mscorlib.System.Collections.ArrayList
 My.Settings.ColumnDisplayOrder = ColumnDisplayOrder;
 My.Settings.Save();
}

No comments:

Post a Comment