DataGridView behave different when created at runtime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • almisba7
    New Member
    • Nov 2008
    • 7

    #1

    DataGridView behave different when created at runtime?

    Hi,
    I am using the follwoing code to generate Menustrip at runtime.
    Code:
    Dim dgv1 As DataGridView
    dgv1 = Me.MenuDataGridView
    ReadMenuInGridView(dgv1)
    Dim MItems As New SortedList(Of String, ToolStripMenuItem)
    For Each dgr As DataGridViewRow In dgv1.Rows
       Dim MItem As New ToolStripMenuItem()
       MItem.Text = dgr.Cells("item_title").Value
       MItem.Tag = dgr.Cells("item_key").Value
       MItems.Add("M" & dgr.Cells("itemid").Value, MItem)
       If dgr.Cells("parent_itemid").Value = 0 Then
          Me.GUMenuStrip.Items.Add(MItem)
          AddHandler MItem.Click, AddressOf Me.MenuClick
       ElseIf MItems.ContainsKey("M" & dgr.Cells("parent_itemid").Value) Then
          MItems.Item("M" & dgr.Cells("parent_itemid").Value).DropDownItems.Add(MItem)
          AddHandler MItem.Click, AddressOf Me.MenuClick
       End If
    Next
    Where the sub "ReadMenuInGrid View" passes the DGV by reference and populates it with the menu item from the database.
    My question is,, the program as it is in the form listed above works fine (where I have a DGV "MenuDataGridVi ew" created at design time), however, if I want to make it 100% dynamic and use a DGV created at run time I will not get the menu? Actually the program will not get in to the For loop?
    Simply to do that we can replace the first 2 statement with:
    Code:
    Dim dgv1 As New DataGridView
    'dgv1 = Me.MenuDataGridView
    Any ideas?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well if you are correctly creating the DataGridView as you see it being created in the designer code, there should be functionaly no difference.

    Comment

    • almisba7
      New Member
      • Nov 2008
      • 7

      #3
      If you mean set the Run-Time-DGV properties right, then the question "what property could cause this"?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by almisba7
        Hi,
        I will not get the menu? Actually the program will not get in to the For loop?
        Simply to do that we can replace the first 2 statement with:
        Code:
        Dim dgv1 As New DataGridView
        'dgv1 = Me.MenuDataGridView
        Any ideas?
        You have to add the dynamic DataGridView to the Form before you can see it.

        Could this be your problem?

        -Frinny

        Comment

        • almisba7
          New Member
          • Nov 2008
          • 7

          #5
          You have to add the dynamic DataGridView to the Form before you can see it.
          I don't want to see the DGV on my form. I am just using it to populate my MenuStrip "GUMenuStri p" from the database. Note that "GUMenuStri p" is created at design time, and I am OK with that.

          Right now I do have the DGV "MenuDataGridVi ew" added to the form at design time, but I have its "Visible" property = False. However, what I want to do is to have the DGV created at runtime instead. But, as I am mentioning in my first post:
          if I want to make it 100% dynamic and use a DGV created at run time I will not get the menu? Actually the program will not get in to the For loop?
          ???

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I would recommend not using a DataGridView simply to load a menu strip with dynamic data. It's over complicated and not necessary.

            Check out this article. It covers how to add menu items dynamically and how to reuse the MenuStrip in several different forms.

            Comment

            Working...