Click to See Complete Forum and Search --> : DataGridView AutogenerateColumns not working in XP


foamy
October 23rd, 2008, 04:24 AM
Hi guys,

One of my applications contains a large datagridview, which is updated every 5 seconds to show data. I have set it up with a selection of columns and have databound it programmatically. In the form's constructor I execute this statement:

grid.AutogenerateColumns = false;
This should prevent the datagridview from showing any other columns than the ones I have defined at design time.
And it does, when the application runs on a Vista machine. As soon as I run it on an XP machine, the datagridview generates the columns anyway.

The datasource is an ArrayList containing custom objects. The objects are of a simple reference type called Customer and they contain the generic properties we all know and love (ID, Name etc.)

I found a solution to this problem. If I put the above statement in the .Designer.cs file, it works on XP. No matter where I put it other than in there, it won't work. It works on Vista either way...

Can someone please explain this behavior? 'Cos I'm thoroughly stumped!

jasonli
October 23rd, 2008, 09:23 AM
How about using bindingSource control for this?

First you set up all columns with DataPropertyName in datagridview, then set datasource to bindingSource control. After you get data, set it to bindingSource.DataSource. It's done!

TheCPUWizard
October 23rd, 2008, 09:29 AM
There should not be any difference in behaviour bases on the operating system.

Are you 100% sure both machines have thew EXACT same version of the .NET framework installed????

foamy
October 24th, 2008, 01:33 AM
Yup, I updated both of them to 3.5 SP1 in an attempt to solve the problem. This had no effect...

I'm just a host of strange problems at the moment :D

EDIT: To answer the bindingsource suggestion, this is how I do it at the moment:

Constructor:

grid.AutoGenerateColumns = false;


Method called from Form_Load()

Customers = driver.GetAllCustomers();
gridCustomers.DataSource = Customers;


I'm not sure if the suggested method of databinding would change anything?

jasonli
October 24th, 2008, 09:12 AM
It's different. Don't set AutoGenerateColumns to false. bindingSource control will manage all columns. If there is no difference whether you use bindingSource or not, what is this control for? It makes display in datagridview very flexible, you can show whatever you want.