views:

12

answers:

0

Hi,

Can anyone explain to me how the ColumnDisplayIndex property is being used when a DataGridView is data bound ?

The reason I ask is this. I have two grids, one above the other. I would like the user to be able to rearrange the order of the columns in the upper grid, and (via the ColumnDisplayIndexChanged event) reflect those reordering changes in the grid below. This would seem simple enough, but the grid is data bound to a DataTable, and this seems to have some very strange effects. Whilst the data binding is going on, columns seem to come and go and this has the effect of firing the ColumnDisplayIndexChanged event many times, often with (to my application) the wrong values.

An example:

public partial class Form1 : Form
{
  DataTable MyDataTable;

  public Form1()
  {
     InitializeComponent();

     MyDataTable = GetMyDataTable();

     this.dataGridView1.ColumnDisplayIndexChanged += ColumnDisplayIndexChangedHandler;
     this.dataGridView1.ColumnAdded += (s, e) => { Console.WriteLine("Column added: {0}", e.Column.Name); };
     this.dataGridView1.ColumnRemoved += (s, e) => { Console.WriteLine("Column removed: {0}", e.Column.Name); };

     this.dataGridView1.DataSource = MyDataTable;
  }

  private void Form1_Load(object sender, EventArgs e)
  {
     //this.dataGridView1.DataSource = MyDataTable;
     DumpColumns("Form1_Load");
  }

  void ColumnDisplayIndexChangedHandler(object sender, DataGridViewColumnEventArgs e)
  {
     Console.WriteLine("{0}_ColumnDisplayIndexChanged: {1} index={2}",((Control)sender).Name, e.Column.Name, e.Column.DisplayIndex);

     DumpColumns("DisplayIndexChanged");
  }

  private DataTable GetMyDataTable()
  {
     DataTable MyDataTable = new DataTable("Data");

     MyDataTable.Columns.Add("One", typeof(int));
     MyDataTable.Columns.Add("Two", typeof(int));
     MyDataTable.Columns.Add("Three", typeof(int));

     MyDataTable.Rows.Add(1, 2, 3);

     return MyDataTable;
  }

  private void DumpColumns(String prefix)
  {
     String columns = "Columns at " + prefix + ":";
     foreach (DataGridViewColumn column in this.dataGridView1.Columns)
     {
        columns += column.Name + ";";
     }
     Console.WriteLine(columns);
  }
}

The results when I run this are as shown below. I cannot seem to reproduce the problem my main application is having (top grid = A,B,C: bottom grid = C,B,A) but the console trace looks wierd enough as it is.

Any ideas ?


Column added: One

Column added: Two

Column added: Three

dataGridView1_ColumnDisplayIndexChanged: Two index=0

Columns at DisplayIndexChanged:Two;Three;

dataGridView1_ColumnDisplayIndexChanged: Three index=1

Columns at DisplayIndexChanged:Two;Three;

Column removed: One

dataGridView1_ColumnDisplayIndexChanged: Three index=0

Columns at DisplayIndexChanged:Three;

Column removed: Two

Column removed: Three

Column added: One

Column added: Two

Column added: Three

dataGridView1_ColumnDisplayIndexChanged: Two index=0

Columns at DisplayIndexChanged:Two;Three;

dataGridView1_ColumnDisplayIndexChanged: Three index=1

Columns at DisplayIndexChanged:Two;Three;

Column removed: One

dataGridView1_ColumnDisplayIndexChanged: Three index=0

Columns at DisplayIndexChanged:Three;

Column removed: Two

Column removed: Three

Column added: One

Column added: Two

Column added: Three

Columns at Form1_Load:One;Two;Three;