it's not adding columns to datagridview?

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi all,

I'm having trouble with a datagridview. I use a datatable to fill it and no matter if I set autogenerate columns true or false, I still get no columns in the datagridview datasource, if there are no rows in the datatable.

The funny thing is that in another form, I use the exactly (or so I think) same code for another datagridview and there it works fine.

(The reason why I need to fill the datagridview's datasource is that I want the columns to show even if there are no rows of data.)

Here's the code:

VB.NET:
Dim params As String() = {"@MyID", varde}
Dim dtDGV As DataTable = GetSqlTable("My_storedproc", params)
dgvMeddelanden.DataSource = dtDGV
dgvMeddelanden.Columns(COL_RUBRIK).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
dgvMeddelanden.Columns(COL_RUBRIK).HeaderCell.ToolTipText = "My message."

The problem is that the datasource doesn't get populated (its column count says 0 even if the datatable column count says 9), and so, the program crashes at dgvMeddelanden.Columns(COL_RUBRIK)... as there are no columns present. It's really strange!

Please help if you can,

Pettrer
 
Last edited:
Yes,

This particular dgv uses only one datatable. Therefore I could do the formatting in design mode instead of in the .vb file, but I use the same kind of code in lots of places and in some of these I need to build the dgv dynamically.

Up until now I have used If dgvMydgv.Rows.Count > 0 Then ... to avoid the crashes, but as a consequence, there are no visible headings using this If statement if no rows are returned. By coincidence I discovered the other day that some of the dgvs actually work without this If statement but I can't figure out why it works sometimes and why it doesn't other times. Therefore I'm hoping for someone who has experienced the same problem (and it's not related to AutoGenerateColumns - when the columnheader text of the first column is going to be set I get this error either way: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". This is not strange in itself as the columncount is zero, even if the datasource.columns.count is more).

So, my problem boils down to this:
dgvMeddelanden.DataSource = ... THIS HAS 9 COLUMNS
' If dgvMeddelanden.Rows.Count > 0 Then
dgvMeddelanden... THIS HAS 0 COLUMNS

As there is no DataBind() in winforms, how come the dgv doesn't get its columns when there are no rows and how do I solve the problem?


I'm puzzled!

Pettrer :)
 
Last edited:
Never having encountered this, can you paste some code that replicates the issue reliably?
 
Hi,

Thanks for replying. Here's some code that explains what's wrong:

VB.NET:
        '(Code for generating a new DataTable from http://www.aspnettutorials.com/tutorials/controls/data-table-vb.aspx)

        Dim myDataTable As DataTable = New DataTable()

        Dim myDataColumn As DataColumn

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "id"
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "username"
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "firstname"
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "lastname"
        myDataTable.Columns.Add(myDataColumn)

        dgvMeddelanden.DataSource = myDataTable

        dgvMeddelanden.ColumnHeadersVisible = True
        dgvMeddelanden.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

What the code above does is to create a datatable, but this dt has no data in it, hence no rows.

Then, the dgv uses this dt as DataSource. I'd hoped that the columns (and columnheaders) would be generated even if there is no data, but that doesn't seem to be the case. As a result, the dgv gets no columns and "dgvMeddelanden.Columns(0).AutoSizeMode" yields a crash.

How do I overcome this problem? In a way, I wouldn't label this a bug, as I'm in fact asking for the data to fill my dgv with, but is there another way to fill the dgv, so that I get the columns even when there are no rows?

/Pettrer
 
I have so far failed to reproduce your issue. I added a DGV to a form, called it dgvMeddelanden, pasted your code and ran the app. the columns showed up okay

I suggest you create a whole new form and make it go, then compare the old form with the new. A tool like BeyondCompare may help for this
 
Back
Top