Connecting Tech Pros Worldwide Help | Site Map

DataGridView behave different when created at runtime?

  #1  
Old June 15th, 2009, 12:29 PM
Newbie
 
Join Date: Nov 2008
Posts: 7
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?
  #2  
Old June 19th, 2009, 09:31 PM
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,096
Provided Answers: 3

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.
  #3  
Old June 20th, 2009, 06:19 AM
Newbie
 
Join Date: Nov 2008
Posts: 7

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"?
  #4  
Old June 26th, 2009, 08:13 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

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
  #5  
Old June 29th, 2009, 11:44 AM
Newbie
 
Join Date: Nov 2008
Posts: 7

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?
???
  #6  
Old June 29th, 2009, 02:34 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940
Provided Answers: 8

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