0

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Andy
  • 616
  • 11
  • 32
  • You are setting the datasource of the bindingsource to the table, but not setting the datasource of the grid itself to anything. – Jeremy May 04 '15 at 16:47
  • @Jeremy - In the With block I set dgvData's .DataSource = binding. Is there more that I must do to make the grid's datasource be the table? – Andy May 04 '15 at 16:51
  • dgvData should be a control on a form (GUI) not a variable. Remove the Dim/Friend statements for dgvData and add a control to the form from the Data folder in the Toolbox. – rheitzman May 04 '15 at 17:27
  • @rheitzmann - shouldn't I be able to create controls and add them to a form programmatically? And like I said, this was working fine using a C1TrueDBGrid (which is a third-party control by ComponentOne). It's just that their control's properties and behaviors are different...so I'm not exactly sure what I'm missing for the DataGridView. – Andy May 04 '15 at 17:46
  • ...Though that did clue me in to getting rid of the "Dim" in the form load event at "Dim dgvData = New System.Windows.Forms.DataGridView. Unfortunately that made no difference. :( – Andy May 04 '15 at 17:56
  • The Declare for dgvData will be in the designer code created when you add the control to the form. If you declare a variable of type DataGirdView it will not appear on the form unless you go to the effort to add it to the container's controls() collection. I use a trick like this to convert RTF to text: declare a RichTextBox as a variable, assign the RTF, then extract the .Text. nothing shows on the screen. – rheitzman May 04 '15 at 19:37
  • Add controls - yes you can, but why? Much easier to use the IDE. My guess is the TrueGrid examples you are looking at is manually adding the control as one may have to do in a non-Visual Studio context. Add the control and give it the name dgvData and my guess is your code will work once you remove your Friend/DIM statements. – rheitzman May 04 '15 at 19:46
  • @rheitzman - I added a note at the end to describe why I need to do it this way as opposed to using the designer. I'm sorry for not specifying that originally. – Andy May 04 '15 at 20:12
  • I suggest you add a dgv to a form, set all its properties for your needs then look at the designer code (The Solution Explorer has a show all files option.) You can then use the designer version as a model. You may want to look into using a FlowlayoutPanel to host the grids - that way you won't have to mess some of the placement issues. If you have a max # it might be easier to add the max and hide the ones you don't need. You may also want to consider an MDI (multi-document interface) approach which would host child forms with one grid per form. – rheitzman May 04 '15 at 22:27
  • A Tabcontrol may also be a reasonable approach - a lot depends on your application and if you need to see all the tables on one screen. – rheitzman May 04 '15 at 22:29

2 Answers2

0

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.

rheitzman
  • 2,247
  • 3
  • 20
  • 36
0

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.

Andy
  • 616
  • 11
  • 32