שתף באמצעות


How to create a datagridview per tab sheet in a TabControl

Question

Wednesday, November 16, 2016 6:48 AM

Can you please write a sub in VB.NET that does this?

( A tab control, on each tab place a DataGridView, for the tab text use the Worksheets name. If the number of sheets is unknown when creating this in the IDE you can dynamically add tabs (and remove them too) at run time. )

Please show us how you dynamically add a tab with a DataGridView on it for each sheet of a excel workbook.

I am so curious :)

All replies (2)

Wednesday, November 16, 2016 11:59 AM

Can you please write a sub in VB.NET that does this?

( A tab control, on each tab place a DataGridView, for the tab text use the Worksheets name. If the number of sheets is unknown when creating this in the IDE you can dynamically add tabs (and remove them too) at run time. )

Please show us how you dynamically add a tab with a DataGridView on it for each sheet of a excel workbook.

I am so curious :)

Please ask a new question in the forum. This question is several years old and is unrelated.

Paul ~~~~ Microsoft MVP (Visual Basic)


Thursday, November 17, 2016 12:00 AM

The following requires a class project named OleDbHelper from this code sample. Placed a TablControl on a form, remove all tabs.

Place the Excel file to read in the Bin\Debug folder. Use the code below.

Imports System.Data.OleDb
Imports OleDbHelper
Public Class Form1
    Private FileName As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sample.xlsx")
    Private Connection As Connections = New Connections
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Utils = New Utility
        Dim SheetsData = Utils.SheetNames(Connection.HeaderConnectionString(IO.Path.GetFileName(FileName)))

        For Each item As SheetNameData In SheetsData
            Dim Page As TabPage = New TabPage() With {.Text = item.DisplayName, .Name = $"tp{item.DisplayName}"}
            Dim dgv As New DataGridView With {.Name = $"dgv{item.DisplayName}", .Parent = Page, .Dock = DockStyle.Fill}
            Dim dt As New DataTable
            Using cn As New OleDbConnection With {.ConnectionString = Connection.HeaderConnectionString(FileName)}
                Using cmd As New OleDbCommand With
                {
                    .CommandText = $"SELECT * FROM [{item.ActualName}]",
                    .Connection = cn
                }
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                    dgv.DataSource = dt

                End Using
            End Using

            TabControl1.TabPages.Add(Page)

        Next
    End Sub
End Class

Sheet names

The code presented will get all sheet name, cycle through them and create a new tab with a DataGridView with WorkSheet rows/columns. 

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator