4

I'm programatically binding a DataGridView's dataset to a List<MyType>. I set the column names and types up in the designer:

enter image description here

When I first bind the list, after populating it, things look great:

enter image description here

But then when I re-bind the dataset to the list (to make it update after repopulating the list) the column names and types seem to be auto-generated from the property names (note the spaces disappear) and types:

enter image description here

My code when populating the List looks like this:

GridView.DataSource = null;
ListOfItems.Clear();
Populate(ListOfItems);
GridView.DataSource = ListOfItems

I'm not very happy about this hacky way of resetting the binding but it doesn't seem to work otherwise, modifying the list contents doesn't refresh the view.

Is this a question of settings on the GridView, or do I need to modify my code?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • I strongly recommend using designer to perform data binding and column creating, Feel free to notify me if you have any question about using designer. – Reza Aghaei Oct 05 '15 at 11:05
  • I think the way that you populate the grid is a different question that is not related to auto generating columns, I will answer it in [Can I control when DataGridView reads and writes from/to its DataSource?](http://stackoverflow.com/questions/32913050/can-i-control-when-datagridview-reads-and-writes-from-to-its-datasource) – Reza Aghaei Oct 05 '15 at 11:30
  • Or if you think its better to ask a new question, let me now:) – Reza Aghaei Oct 05 '15 at 11:32
  • I think they're related but separate questions :) – Mr. Boy Oct 05 '15 at 11:37

3 Answers3

5

You should create and add column yourself and set AutoGenerateColumns to false to prevent generating unwanted columns.

this.dataGridView1.AutoGenerateColumns = false;

If you use windows forms designer to perform data binding and creating columns, you will benefit from standard generated codes. But if you want to generate that code your self, you should take care of multiple aspect of your code.

For example, when you use a binding source, set the data source of grid to that binding source, the designer will look at columns that the binding source exposes and adds those columns to your grid at design time and also adds that line of code to disable auto generation of columns.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I'm using the designer to create the columns already. Although AutoGenerateColumns doesn't seem accessible through designer. I set up the data source in code because I'm not sure how to do this using designer (yet). It seems this property _is_ the fix to my specific problem though, thanks. – Mr. Boy Oct 05 '15 at 11:15
  • @Mr.Boy That property can not be set from designer. You should set it in code. – Reza Aghaei Oct 05 '15 at 11:21
2

Yes, setting the DataSource will cause the grid to regenerate the columns.

Use attributes on your model or rename the columns from code: Custom column names for DataGridView with associated DataSource.

You can also disable automatic column generation. Then you'll have to make sure the column's names map to your property names.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • This won't help with the fact my column type is changing as well - look at `Severity` column. Plus it just seems like one hack on top of another, I feel there should be a 'proper' way to do this ;) – Mr. Boy Oct 05 '15 at 10:13
-1

Just don't null your GridView.DataSource.

Comment this line

\\GridView.DataSource = null;
MarmiK
  • 5,639
  • 6
  • 40
  • 49