Connecting Tech Pros Worldwide Forums | Help | Site Map

DataGridView behave different when created at runtime?

Newbie
 
Join Date: Nov 2008
Posts: 7
#1: Jun 15 '09
Hi,
I am using the follwoing code to generate Menustrip at runtime.
Expand|Select|Wrap|Line Numbers
  1. Dim dgv1 As DataGridView
  2. dgv1 = Me.MenuDataGridView
  3. ReadMenuInGridView(dgv1)
  4. Dim MItems As New SortedList(Of String, ToolStripMenuItem)
  5. For Each dgr As DataGridViewRow In dgv1.Rows
  6.    Dim MItem As New ToolStripMenuItem()
  7.    MItem.Text = dgr.Cells("item_title").Value
  8.    MItem.Tag = dgr.Cells("item_key").Value
  9.    MItems.Add("M" & dgr.Cells("itemid").Value, MItem)
  10.    If dgr.Cells("parent_itemid").Value = 0 Then
  11.       Me.GUMenuStrip.Items.Add(MItem)
  12.       AddHandler MItem.Click, AddressOf Me.MenuClick
  13.    ElseIf MItems.ContainsKey("M" & dgr.Cells("parent_itemid").Value) Then
  14.       MItems.Item("M" & dgr.Cells("parent_itemid").Value).DropDownItems.Add(MItem)
  15.       AddHandler MItem.Click, AddressOf Me.MenuClick
  16.    End If
  17. Next
  18.  
Where the sub "ReadMenuInGridView" 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 "MenuDataGridView" 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:
Expand|Select|Wrap|Line Numbers
  1. Dim dgv1 As New DataGridView
  2. 'dgv1 = Me.MenuDataGridView
  3.  
Any ideas?

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#2: Jun 19 '09

re: DataGridView behave different when created at runtime?


Well if you are correctly creating the DataGridView as you see it being created in the designer code, there should be functionaly no difference.
Newbie
 
Join Date: Nov 2008
Posts: 7
#3: Jun 20 '09

re: DataGridView behave different when created at runtime?


If you mean set the Run-Time-DGV properties right, then the question "what property could cause this"?
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#4: Jun 26 '09

re: DataGridView behave different when created at runtime?


Quote:

Originally Posted by almisba7 View Post

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:

Expand|Select|Wrap|Line Numbers
  1. Dim dgv1 As New DataGridView
  2. 'dgv1 = Me.MenuDataGridView
  3.  
Any ideas?

You have to add the dynamic DataGridView to the Form before you can see it.

Could this be your problem?

-Frinny
Newbie
 
Join Date: Nov 2008
Posts: 7
#5: Jun 29 '09

re: DataGridView behave different when created at runtime?


Quote:
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 "GUMenuStrip" from the database. Note that "GUMenuStrip" is created at design time, and I am OK with that.

Right now I do have the DGV "MenuDataGridView" 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:
Quote:
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?
???
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,719
#6: Jun 29 '09

re: DataGridView behave different when created at runtime?


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.
Reply