DataGridView bound to DataTable is not showing

2.1k Views Asked by At

I am trying to show a DataGridView in a form that is bound to a DataTable, but it's not showing up. I was doing this using a C1TrueDBGrid and that was working...I decided to switch to using a DataGridView due to some complications with the TrueDBGrid. Can anyone help me figure out why nothing is showing?

In the form I declare these:

Public binData As DataSet
Friend WithEvents dgvData As System.Windows.Forms.DataGridView

The binData is filled with tables created via a separate calculation routine. Then this is the form load event:

    'create a tab page and add data grid view for every table in the set
    For i = 0 To binData.Tables.Count - 1
        Dim tabPage As C1.Win.C1Command.C1DockingTabPage = New C1.Win.C1Command.C1DockingTabPage
        tabPage.Text = binData.Tables(i).TableName
        tabContent.TabPages.Add(tabPage)

        Dim dgvData = New System.Windows.Forms.DataGridView
        Dim binding As New BindingSource
        binding.DataSource = binData.Tables(i)

        With dgvData
            .Dock = DockStyle.Fill
            .AllowUserToOrderColumns = False
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
            .DataSource = binding
            .AutoGenerateColumns = True
        End With
        tabPage.Controls.Add(dgvData)
    Next   'DataTable In binData.Tables

When the form loads, the tab pages are there and labeled as expected, but they look empty (no table).

I did try instead setting the DataSource to the DataSet called binData (as opposed to a specific table), and then setting dgvData's DataMember property to the name of the specific table I want to display in it...that made no difference.

Note: I need to be able to do this programmatically at runtime as opposed to using the visual designer because I do not know the exact number of grids I need until the form loads with a particular dataset - the dataset it gets can have a different number of tables depending on what the user wants.

2

There are 2 best solutions below

0
On

Here's some rough code to add dgvs to a flow panel:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Static dgvCount As Integer
    dgvCount += 1
    Dim dgvNew As New DataGridView
    dgvNew.Width = DataGridView1.Width
    dgvNew.Height = DataGridView1.Height
    dgvNew.Name = "dgv" & dgvCount
    ' clone other properties as need
    FlowLayoutPanel1.Controls.Add(dgvNew)
    Debug.Print(FlowLayoutPanel1.Controls(FlowLayoutPanel1.Controls.Count - 1).Name)
End Sub

Starts with one dgv - DataGridView1 as the model for properties. Anchoring is not working in the flow panel so some custom code may be need to change width.

Flow panel doesn't scroll so may not be the best choice - look into TableLayout as a possibility. TabControl is another option.

0
On

OK, well it turns out there was nothing wrong with what I was doing. The issue turned out to be in one line of code that has nothing to do with binding the DGV's datasource or anything.

ComponentOne has a control called a ThemeController in which you can set themes for your forms and the controls within. I had a line of code to set the theme for dgvData to my default application theme (which sets visual style and details regarding colors, fonts, etc.). For whatever reason, THAT was rendering my grid non-visible. I will be logging a ticket with them.