12 Replies - 226 Views - Last Post: 05 December 2018 - 01:33 AM Rate Topic: -----

#1 jacobusduvenhage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 18

No datagrid in dynamically created tab

Posted 03 December 2018 - 11:43 PM

I create a tab page dinamically then read a csv file into the tab. But there Is no data grid vissible
Full Code
//
Dim strFileSize As String = ""
        Dim di As New IO.DirectoryInfo("C:\Users\Cobus\OneDrive\Venessa\Accounts\CBD 2018\")
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.csv")
        Dim fi As IO.FileInfo
        Dim NewTab As TabControl = New TabControl
        Dim NewTabpage As TabPage = New TabPage
        Dim DataGrid1 As New DataGridView

        Dim HeaderLine As String = ""
        Dim ColHeaders As Array
        Dim NrOfCols As Integer = 0
        Dim TextLine As String = ""
        Dim Items As Array
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        Try

            'Dim objReader As New System.IO.StreamReader(FILE_NAME)
            For Each fi In aryFi
                TextLine = ""

                NewTabpage.Text = Mid(fi.Name, 1, Len(fi.Name) - 4)
                NewTab.TabPages.Add(NewTabpage)
                NewTab.Dock = DockStyle.Fill
                Me.Controls.Add(NewTab)
                Dim File_Name As String = di.ToString & fi.Name

                'DataGrid1.Name = Mid(fi.Name, 1, Len(fi.Name) - 4)
                Me.Controls.Add(DataGrid1)
                'DataGrid1.Name = Mid(fi.Name, 1, Len(fi.Name) - 4)
                'If the user clicked the first columns is headers then we do so
                HeaderLine = objReader.ReadLine()
                ColHeaders = Split(HeaderLine, ",")
                NrOfCols = UBound(ColHeaders)
                For Each value In ColHeaders
                    Dim col As New DataGridViewTextBoxColumn

                    col.DataPropertyName = value
                    col.HeaderText = value
                    col.Name = value
                    DataGrid1.Columns.Add(col)
                Next


                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    'Lets just skip empty lines
                    If Trim(TextLine) <> "" Then
                        'splitting the textlines
                        Items = Split(TextLine, ",")
                        HeaderLine = objReader.ReadLine()
                        ColHeaders = Split(HeaderLine, ",")
                        NrOfCols = UBound(ColHeaders)
                        For Each value In ColHeaders
                            Dim col As New DataGridViewTextBoxColumn

                            col.DataPropertyName = value
                            col.HeaderText = value
                            col.Name = value
                            DataGrid1.Columns.Add(col)
                        Next
                        Dim n As Integer = DataGrid1.Rows.Add()
                        'If the nr of columns is less than the columncount we just leave it blank, so we only use the UBound(items)
                        'If it contains more columns it skips them anyway
                        Dim UsedColumns As Integer = 0
                        If DataGrid1.Columns.Count > UBound(Items) + 1 Then
                            UsedColumns = UBound(Items)
                        Else
                            UsedColumns = DataGrid1.Columns.Count - 1
                        End If
                        For x = 0 To UsedColumns
                            'put each item in a cell
                            DataGrid1.Rows.Item(n).Cells(x).Value = Items(x)
                        Next x
                    End If
                    DataGrid1.Visible = True
                Loop
            Next
            objReader.Close()
            objReader.Dispose()
            Exit Sub
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try


///
norelated solution searches come up wit request " no data grid in tab"
No errors
I tried and studied the piece that I read the csv and the program reads the file it seems correctly. I also add the datagrid. But no data grid apears in tab.
It seems that I have to "link, point" datagrid to the tab page but for the live of me I do not have an idea how
regards
Cobus

This post has been edited by andrewsw: 04 December 2018 - 07:31 AM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: No datagrid in dynamically created tab

#2 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,569
  • Joined: 07-September 13

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 01:22 AM

Your adding the DataGrid1 to the me.Controls, instead of the Tabpage.Controls
Me.Controls.Add(DataGrid1)

Was This Post Helpful? 0
  • +
  • -

#3 jacobusduvenhage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 18

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 07:11 AM

View Postmaceysoftware, on 04 December 2018 - 01:22 AM, said:

Your adding the DataGrid1 to the me.Controls, instead of the Tabpage.Controls
Me.Controls.Add(DataGrid1)


If I use
/
NewTab.Controls.Add(DataGrid1)
/

I get error message
Only tab pages can be added to tab controls

If I use
/
NewTabpage.Controls.Add(DataGrid1)
/
No errormessage but still no datagrid in tab page

Thanks for the help so far
Cobus
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 07:32 AM

My guess would be that you add the grid to the page then add the page to the tab control.
Was This Post Helpful? 0
  • +
  • -

#5 jacobusduvenhage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 18

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 07:51 AM

View Postandrewsw, on 04 December 2018 - 07:32 AM, said:

My guess would be that you add the grid to the page then add the page to the tab control.

Hi Andrew
Thanks so far I modified code like this
/
Me.Controls.Add(DataGrid1)
NewTabpage.Text = Mid(fi.Name, 1, Len(fi.Name) - 4)
NewTab.TabPages.Add(NewTabpage)
NewTab.Dock = DockStyle.Fill
Me.Controls.Add(NewTab)
Dim File_Name As String = di.ToString & fi.Name

'DataGrid1.Name = Mid(fi.Name, 1, Len(fi.Name) - 4)

NewTabpage.Controls.Add(DataGrid1)
/
Still do not seem to work I get the same as what I declared in the beginning.
Could you perhaps give me an example how to add datagrid to tabpage
Regards
Cobus
Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,569
  • Joined: 07-September 13

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 07:53 AM

For reference, the second attempt is the correct one.

I noticed on line 74 you set the visible flag on the datagridview to true, have you put a breakpoint on this line to make sure it is being hit?

Is the grid invisible before that? could the issue be that the grid isn't becoming visible?

After you add the datagridview to the tabpage can you set the visible flag to true straight away, when you run the code, does the datagridview now appear on the tabpage?
Was This Post Helpful? 0
  • +
  • -

#7 jacobusduvenhage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 18

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 08:03 AM

View Postmaceysoftware, on 04 December 2018 - 07:53 AM, said:

For reference, the second attempt is the correct one.

I noticed on line 74 you set the visible flag on the datagridview to true, have you put a breakpoint on this line to make sure it is being hit?

Is the grid invisible before that? could the issue be that the grid isn't becoming visible?

After you add the datagridview to the tabpage can you set the visible flag to true straight away, when you run the code, does the datagridview now appear on the tabpage?

Hi Macey
I have changed the code to this
/
NewTabpage.Controls.Add(DataGrid1)
DataGrid1.Visible = True
/
Visible does hit the break point still no datagrid vissible
I'm also trying to google how to get the datagrid into the tab page
Thanks for the help
As you can see on screen capture I do create the tab pags but they stay empty.
I'm sure it is something simple but not so simple to me. I haveing a go at it for +- 1.5 days
Regards
cobus
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 08:15 AM

I would skip the tab control for a minute and just see if the DGV appears on the form area. If so, then you'd know that it is a problem with the tab control.

Worth mentioning is that common issues when a control doesn't appear are:
It is not Visible,
It is sitting behind another control,
It has no dimensions,
It is off-screen,
The control is not the one you think it is ;)
Was This Post Helpful? 0
  • +
  • -

#9 jacobusduvenhage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 18

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 08:22 AM

View Postandrewsw, on 04 December 2018 - 08:15 AM, said:

I would skip the tab control for a minute and just see if the DGV appears on the form area. If so, then you'd know that it is a problem with the tab control.

Worth mentioning is that common issues when a control doesn't appear are:
It is not Visible,
It is sitting behind another control,
It has no dimensions,
It is off-screen.

Hi Andrew
Before I tried putting into the tab page I can create the data grid and it is visible. It could be a dimensions issue I will check it out. worth mentioning is that if I hit break point it shows me that the program is accessing the tab page
Regars
Cobus
Was This Post Helpful? 0
  • +
  • -

#10 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,569
  • Joined: 07-September 13

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 09:13 AM

Hello Jacob,

What I would do is it dumb it down a little, create a new project and try to accomplish what you are trying to do in that app, just this one thing, forget about everything else. forget about putting columns in the grid, or any of the other logic.

Dynamically create a Tab Control
Add Tab Control to Form.
Dock Tab Control
Dynamically Create a Tab Page
Add Tab Page to Tab Control
Add DataGridview to Tab Page Controls.

Does it work? Yes then move that code back over to your application, comment out everything else, does it work?

Start to comment in little bits of your code at a time to see if it keeps working. Currently, there is just too much code around and with quick glazes, I can't see anything other than what I have mentioned.

I have even knocked the below up and that works like a trooper, therefore it has to be something in your application not making it work like Andrew said visibility, dimensions something like that.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim tabControl As New TabControl

        Me.Controls.Add(tabControl)
        tabControl.Dock = DockStyle.Fill

        Dim tabPage As New TabPage

        tabControl.TabPages.Add(tabPage)

        Dim datagrid As New DataGridView
        tabPage.Controls.Add(DataGridView1)
    End Sub


You can try setting the location of the control after adding it to DataGrid1.Location = new Point(0,0) and the size to DataGrid1.Size = new size(50,50)

That will make sure the control is in the bounds of the control and that I has a size
Was This Post Helpful? 0
  • +
  • -

#11 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • Posts: 1,569
  • Joined: 07-September 13

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 09:19 AM

  Dim datagrid As New DataGridView
    tabPage.Controls.Add(DataGridView1


I should point out this part of the code, originally I was dynamically creating the DataGridView, then I swap to an already existing DataGridView, both worked for me.
Was This Post Helpful? 0
  • +
  • -

#12 jacobusduvenhage   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-December 18

Re: No datagrid in dynamically created tab

Posted 04 December 2018 - 09:58 PM

Thanks Guys
Seems that I'm getting there.Not functional yet but found error in my code. I do get an error message of fulwiegth exceeding.But I can see why. Will carry on
Thanks Again
Regards
Cobus

This post has been edited by andrewsw: 05 December 2018 - 01:33 AM
Reason for edit:: removed previous quote, just use the Reply button

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: No datagrid in dynamically created tab

Posted 05 December 2018 - 01:33 AM

You do not need to quote a post every time, there is a Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1