none
How would is see a table in Form1 when program is not running RRS feed

  • Question

  • Public Class Form1
        Private DataGridView1 As New DataGridView With {.Dock = DockStyle.Fill}
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "C1"})
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "C2"})
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "C3"})
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "C4"})
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "C5"})
            DataGridView1.Columns.Add(New DataGridViewTextBoxColumn With {.Name = "C6"})
    
            DataGridView1.RowHeadersVisible = False
    
            DataGridView1.Rows.Add("", "", "", "", "")
    
            DataGridView1.Rows.Add("", "", "", "", "")
    
            DataGridView1.Rows.Add("", "", "", "", "")
    
            DataGridView1.Rows.Add("", "", "", "", "")
    
            DataGridView1.Rows.Add("", "", "", "", "")
    
            DataGridView1.Rows.Add("", "", "", "", "")
            DataGridView1.AllowUserToResizeRows = False
    
            DataGridView1.AllowUserToResizeColumns = False
    
            Controls.Add(DataGridView1)
        End Sub
    End Class

    Tuesday, October 2, 2018 9:03 PM

All replies

  • Hi

    What on earth do you mean? If you project is not running then why would you expectto see the Form/DataGridView?


    Regards Les, Livingston, Scotland

    Tuesday, October 2, 2018 9:06 PM
  • As coded you can not see the DataGridView on the form canvas. The only way to see the DataGridView is to create it by dropping a DataGridView from the toolbox onto the form's canvas.

    If that is what you want, remove  the following. When doing so all the remaining code will have errors, ignore them.

      Private DataGridView1 As New DataGridView With {.Dock = DockStyle.Fill}

    Remove this line of code

    Controls.Add(DataGridView1)

    Now drop a DataGridView onto the form followed by selecting the chevron in the top right corner of the form, there will be an option to dock to parent, select it.

    Build the project.


    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
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 2, 2018 11:38 PM
    Moderator
  • How would is see a table in Form1 when program is not running

      Run the program.  8)

     **********************

     Seriously though,  I think what you might be trying to ask is how to have those columns/rows added to the DataGridView while you are in design mode.  If that is what you mean,  then you can create your own small class that inherits from the DataGridView class.  Then you can add the columns/rows when your DataGridView class is being constructed.

     Below I show an example of a small DataGridView class called Dgv.  It calls the CreateTable sub when it is being created which adds the columns/rows by setting the Dgv's DataSource to a DataTable.  You could add the columns/rows by just adding them directly to the Dgv,  without using a DataTable as I do.

     After adding the Dgv class,  I Build the project,  and then add a Dgv to the form from the ToolBox.

    Public Class Dgv
        Inherits DataGridView

        Public Sub New()
            Me.Size = New Size(604, 178)
            Me.RowHeadersVisible = False
            Me.AllowUserToResizeRows = False
            Me.AllowUserToResizeColumns = False
            Me.Dock = DockStyle.Fill
            CreateTable()
        End Sub

        Private Sub CreateTable()
            Dim dt As New DataTable
            With dt
                .Columns.Add("C1")
                .Columns.Add("C2")
                .Columns.Add("C3")
                .Columns.Add("C4")
                .Columns.Add("C5")
                .Columns.Add("C6")
                .Rows.Add("", "", "", "", "")
                .Rows.Add("", "", "", "", "")
                .Rows.Add("", "", "", "", "")
                .Rows.Add("", "", "", "", "")
                .Rows.Add("", "", "", "", "")
                .Rows.Add("", "", "", "", "")
            End With
            Me.DataSource = dt
        End Sub
    End Class
     

     This is what it looks like in the form's designer window when I add a Dgv control to the form...


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, October 2, 2018 11:45 PM
    Tuesday, October 2, 2018 11:43 PM