Well, I was very happy to be able to set up a complex query to fill a DataSet and use that DataSet as a DataSource for a DataGridView. But the issue is that the DataGridView displays the data in a very raw mode and that I need to apply many different style depending on each cell.
But the Columns property returns null and the Columns.Count return 0
Same for Rows and Count.
I have had to create all the columns manually from code to be able to apply styles to columns, but now I need to apply styles to cells/rows and I can't see any way of getting that Rows collection...
Is this my poor knowledge Is this a bug Is there a workaround
Thanks for your help and time!

DataGridView , DataSource and myGridView.Rows return null (same for .Columns)
z e n
Dim ds As DataSet = GetDataSet()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers"
MessageBox.Show(DataGridView1.Rows.Count)
and it was giving me the correct number of rows. AFAIK the dataset should be populated before hand or atleast the schema should be there.
Well since I cant repro your problem I cant help you out much. But the correct sequence is as above, populate the dataset, set the DataSource and then the DataMember property.
Vic02
Note that this assumes that you have the Northwind database handy:
1) New Windows Forms app
2) Click The Add new datasource option and connect to the Northwind database
3) Select the Products tables and finish the wizard
4) Drag and drop the Products table from the data sources window to your form to create and databind a DataGridView
5) Attach an event handler for the form's load event
6) Add code to show a messagebox with the datagridview's ColumnCount and RowCount
7) Run
Hope this helps,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
luvmyduc
What's your default configuration for the datagridview
what does that GetDataSet() function do exactly
Joao Rocha
I just gave it another chance, and it's still empty.
I don't understand.
The fact that the DataGridView shows the data means that my code should be ok. My guess is that there's a bug which doesn't map the data to the actual Rows and Columns of the grid.
If I create programmatically the columns of the DataGridView, I get the right number of columns, but still 0 as number of Rows.
I'm using the VS2005 beta2.
I want to cry :-(
nolim8ts
It sounds like the DataGridView isn't actually databound since it doesn't show any rows or columns.
You might need to set the DataMember for the DataGridView or use a BindingSource component and bind your datasource to it and the DataGridView to the BindingSource. You can use the designer to do this.
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Teem60004
The GetDataSet just connects to a database and retrieves a dataset. The configuration for the datagridview was default as it comes when you place a datagrid control on the form.
rage
In the constructor, I create the DataSet and the DataGridView, then I assign those properties (DataSource and DataMember).
Later I populate the DataSource
Then, once the data source is populated, I try to get the Rows collection in order to apply the style.
I tried populating the DataSet then assigning the DataSource and the DataMember, but I get the same results (and in my understanding, I should get the same results. Let me know if I'm wrong).
The DataSource.getTable.Rows.Count and .Columns.Count give me the right number of items and I can go through that data. For example.
foreach (MonitorsDataSet.fundsInFoFTableRow row in _fofDataSet.fundsInFoFTable.Rows) {
foo.bar(); //this works !
}
Once again, the DataGridView displays all the data properly. But I just need to be able to apple row styles and cell styles, otherwise it's just useless :-(
Thank you very much for your help!
Bassel Samir Banbouk
Now, I have created a public applyStyles() method which I call after having added the various controls to their parents' Controls list and this works.
But I don't like this public method which shouldn't be visible at all, in the way I have designed this class.
So I tried calling the CreateControl() method on both the DataGridView and the TabPage in which I'm adding it. It didn't work ( I also called the method on the main TabControl, so this makes 3 calls).
Could you please let me know what I'm doing wrong and how to fix this
Could you also please point me to the relevant documentation and information about how the controls are created/instanciated so that I get to understand this behaviour
Thank you very much for your help!
Blanca Flores
Could this be the reason of my issues, if the DataGridView rows/columns are not created as long as the DataGridView itself is not displayed on screen by the main form I doesn't look like it, from my tests.
Otherwise, the only other difference I see is that I don't have a TableAdaptor, and I fill the DataSet with custom queries and computing.
Andrei Csibi
www.etramway.com
The handle isn't created all the way if the control isn't added to a form (or other control such as a usercontrol) so you'll have to do that before can become fully databound.
Hope this helps,
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Mark Garner
But in my own program, I create the DataSource manually and I populate the DataSet manually too, because I need to compute some of the fields I'm displaying.
I'll investigate a little more and try to find what are the differences in configuration for the DataSet, DataSource and DataGridView and I'll post another time if I find anything relevant ...or not.
DLSeth
It's just when it comes to getting the rows that it returns a null reference.
Another other tip
It seems to be fairly easy to reproduce:
- create and populate a dataset
- create a datagridview, set it's datasource to the dataset and the data
Here are snipets fo my code:
_fofDataSet = new MonitorsDataSet();
_fofDataGridView = new DataGridView();
// [...]
_fofDataGridView.DataSource = _fofDataSet;
_fofDataGridView.DataMember = _fofDataSet.fundsInFoFTable.TableName;
Then, if I try to process the rows, with a "foreach" loop, well, the loop doesn't even start because the Rows property returns "null":
foreach (DataGridViewRow currentRow in _fofDataGridView.Rows) {
fakeFunction.displaySomething()
//this function is never called....
}
Thanks for your help