OK, time to hack it:
To demonstrate, here's a little app:
public partial class Form1 : Form
{
DataTable table = new DataTable();
public Form1()
{
InitializeComponent();
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
this.dataGridView1.ColumnAdded += new DataGridViewColumnEventHandler(dataGridView1_ColumnAdded);
}
void dataGridView1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
Console.WriteLine("Column added");
e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
private void button1_Click(object sender, EventArgs e)
{
table.Columns.Add("Name");
table.Columns.Add("Age", typeof(int));
table.Rows.Add("John", 27);
this.FlipSelectionMode();
this.dataGridView1.DataSource = table;
this.FlipSelectionMode();
}
private void button2_Click(object sender, EventArgs e)
{
this.FlipSelectionMode();
table.Columns.Add("Height",typeof(int));
table.Rows[0]["Height"] = 60;
this.FlipSelectionMode();
}
private void FlipSelectionMode()
{
this.dataGridView1.SelectionMode = this.dataGridView1.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect ? DataGridViewSelectionMode.CellSelect : DataGridViewSelectionMode.ColumnHeaderSelect;
}
}
Basically, at first I set the DataGridView selection mode to ColumnHeaderSelect. On the button1 click, I add stuff to the datatable, and then bind it to the DataGridView. The trick is, that I call a method called FlipSelectionMode() before and after I bind the DGV. What this does, is if it's in columnheaderselect mode it flips it to cell select and vice versa. That enables the column to be added. Then, in the column added event, I set the columns sort property to Programmatic, otherwise, you won't be able to add another column. To demonstrate that, on the button2 click, it just adds another column, again while flipping the selection mode before and after.
I agree, this is a total hack, but the DGV is funky. I've had many many issues with it, and I almost always have to hack around stuff.