2
votes

I have looked and found one question which was similar, though did not resolve my issue - that other question was datagridview-is-blank-after-changing-datasource-at-runtime

I am sure this is a simple question with an even more simple answer..Working on my first Windows Forms application and finding some things a lot different from ASP.Net :)

So in this application I have the following:

  • Main Form
  • Toolbar on this form, opens up a second form to set application settings
  • This second form has a tab control (2 tabs) with various textboxes/checkboxes/combolists/buttons
    • All of these work perfectly.
  • On the second tab of this second form, along with more textboxes etc, I have added a DataGridView; called printerListGrid

In the code-view of this form I am trying to bind a List(Of Printer) to it

printerListGrid.DataSource = AppContext.printers.printerList

When I run my app and go to the form/tab the DataGridView is blank.

I have also tried:

    printerListGrid.Columns.Clear()
    printerListGrid.AutoGenerateColumns = True
    printerListGrid.DataSource = AppContext.printers.printerList

Again - it's blank.

While debugging I can see that my printerList contains 2 printer objects. I can also see that these have been set to the printerListGrid by viewing the contents of the .DataSource property. Yet, the DataGridView is still blank.

I'm guessing there must be some way to re-render the grid?

For the sake of code-discolsure; this is what is in the List(Of Printer)

    Public Structure Printer
        Public id As Integer
        Public name As String
        Public material As Enums.Materials
        Public outputFolder As String
    End Structure

Let me know if there is any more code/description you need to provide help.

UPDATE:

I forgot to mention. I am trying to set the DataSource in a Private Sub which is called on the forms Load event MyBase.Load

UPDATE #2

I continue to play with this, trying to get it to work - I might be totally misunderstanding the control and its purpose.. anyway, I have also tried the following:

    printerListGrid.AutoGenerateColumns = False

    Dim col As DataGridViewColumn = New DataGridViewTextBoxColumn()
    col.DataPropertyName = "name"
    col.HeaderText = "Name"
    printerListGrid.Columns.Add(col)

    col = New DataGridViewTextBoxColumn()
    col.DataPropertyName = "material"
    col.HeaderText = "Material"
    printerListGrid.Columns.Add(col)

    col = New DataGridViewTextBoxColumn()
    col.DataPropertyName = "outputFolder"
    col.HeaderText = "Output Folder"
    printerListGrid.Columns.Add(col)

    printerListGrid.DataSource = AppContext.printers.printerList

This results in at least the grid showing rows; appears like 2. Yet they are not populated with the data from List(Of Printer)...

2
I usually do this with a bindingsource, but for some reason it wont work today. Basically you set the list as data source in the bindingsource. Then you set the Datagridview Datasource to be the bindingsource. This way you can run Bindingsource.Resetbindings(true) which should update your datagridview. But I can't create a working example :/ - WozzeC
Quite ironic eh :) and yep, I've tried that too. I'm also trying to bind the same List to a combo box. It binds, but the DataMember and ValueMember won't set. The list just shows 2 items reading AutoArtwork.Logic.Printer - I've tried setting these to all sorts. This is really irritating me today :) - Darren Wainwright
Got thrown by the fact that I allways use properties. I'm not a big fan of global variables, and today my brain believed that Public and Property where the same thing. - WozzeC
I did too. in ASP.Net you can get away with Public; I've never ran into this problem so wasn't aware of the difference. Thanks again for spotting it :) - Darren Wainwright

2 Answers

4
votes

Found out what I did wrong. Basically try this:

Public Structure Printer
    Property id As Integer
    Property name As String
    Property material As Enums.Materials
    Property outputFolder As String
End Structure
2
votes

The fields that are being bound need to be properties, not public variables. For example, if you replace your structure with the following you will see the id's in the grid. You'd have to do the same property switch for your other variables as well to have them show up.

 Public Structure Printer
        Private _id As Integer
        Public Property id() As Integer
            Get
                Return _id
            End Get
            Set(ByVal value As Integer)
                _id = value
            End Set
        End Property

        Public name As String
        Public material As Materials
        Public outputFolder As String

    End Structure