How to set DataGridView column properties for automatically generated columns?

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.

Rows0"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.

OK this is hackish but does work, +1 – Brendan Feb 7 '09 at 15:00.

Interesting - I've done a lot with DataGridView and I haven't seen this. Perhaps the pragmatic option might be to build the columns manually? Probably not what you want to hear...

I am trying not to use the designer since the data I'm reflecting is for use with arbitrary sized data sets of doubles and so the number of columns changes a lot. – Brendan Feb 6 '09 at 17:12 Programatically - by looping over the view columns. – Marc Gravell?

Feb 6 '09 at 19:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions