Host a DataGridView on a ContextMenuStrip using ToolStripControlHost

Discussion in 'Visual Basic .Net' started by DN Admin, Apr 27, 2012.

  1. DN Admin

    Staff Member

    Country:
    United States
    Joined:
    Aug 7, 2010
    Location:
    Joliet, IL U.S.A.
    I'm trying to show a small DataGridView in my ContextMenuStrip that I have built. The DataGridView will show a unique list of values in a DataGridViewTextboxColumn "Sample ID" when its column header is right-clicked. I can get the unique values
    just fine, but I'm having trouble displaying the small DataGridView in the ContextMenuStrip. It currently looks like this.
    <img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/99322
    Notice, there is no data in the DataGridView. I have confirmed that the DataGridView.DataSource has several rows in it, yet the DataGridView shows nothing. Plus, I need to increase the size of my DataGridView, thus increasing the size of the
    ContextMenuStrip to show the DataGridView.
    Here is my code (sorry if its too much, just wanted to make sure you see the whole picture):
    <pre class="prettyprint lang-vb Public Class ContextMenu_Column_String
    Inherits ContextMenuStrip

    Private dvs As DataGridViewSettings
    Private dgv As DataGridView
    Private ht As DataGridView.HitTestInfo
    Private dfs As DataGridViewFilterAndSorter

    Public Sub New(ByVal sender As DataGridViewSettings, ByVal e As System.Windows.Forms.MouseEventArgs)

    Me.dvs = CType(sender, DataGridViewSettings)
    Me.dgv = Me.dvs.DataGridView
    Me.ht = Me.dgv.HitTest(e.X, e.Y)
    Me.dfs = Me.dvs.DataGridViewFilterAndSorter

    Dim dtUniqueValues As DataTable = Me.dfs.MasterDataTable.Copy.AsDataView.ToTable(True, Me.dgv.Columns(ht.ColumnIndex).DataPropertyName)
    dtUniqueValues.Columns.Add("Select", Type.GetType("System.Boolean"))
    Dim dv As DataView = dtUniqueValues.AsDataView
    dv.Sort = Me.dgv.Columns(ht.ColumnIndex).DataPropertyName

    Dim chk As New DataGridViewCheckBoxColumn
    With chk
    .Name = "Select"
    .Width = 25
    .DataPropertyName = "booSelect"
    End With

    Dim tex As New DataGridViewTextBoxColumn
    With chk
    .Name = "Values"
    .Width = 100
    .DataPropertyName = Me.dgv.Columns(ht.ColumnIndex).DataPropertyName
    End With

    Dim dgvUnique As New DataGridView

    dgvUnique.Columns.Add(chk)
    dgvUnique.Columns.Add(tex)
    dgvUnique.Width = 125
    dgvUnique.CellBorderStyle = DataGridViewCellBorderStyle.None
    dgvUnique.ColumnHeadersVisible = False
    dgvUnique.AutoGenerateColumns = False
    dgvUnique.DataSource = dtUniqueValues

    Dim CMS As New ContextMenuStrip
    Dim CM_SubMenu As New ToolStripMenuItem()
    Dim IC As New IconConverter

    With CMS.Items
    .Add(New ToolStripMenuItem("Copy", IC.ConvertTo(My.Resources.copy, GetType(Image)), New EventHandler(AddressOf Me.CopySelectedCells), "nmCopy"))
    .Add(New ToolStripSeparator)
    .Add(New ToolStripMenuItem("Sort A to Z", My.Resources.SortHS, New EventHandler(AddressOf Me.SortAZ), "nmSortAZ"))
    .Add(New ToolStripMenuItem("Sort Z to A", Nothing, New EventHandler(AddressOf Me.SortZA), "nmSortZA"))
    .Add(New ToolStripMenuItem("Save My Sortings", Nothing, New EventHandler(AddressOf Me.SaveMySortings)))
    .Add(New ToolStripSeparator)
    .Add(New ToolStripMenuItem("Fill Column With ...", Nothing, New EventHandler(AddressOf Me.FillColumnWith), "nmFillColumnWith"))
    .Add(New ToolStripSeparator)
    .Add(New ToolStripMenuItem("Hide Column", Nothing, New EventHandler(AddressOf Me.HideColumn)))
    .Add(New ToolStripMenuItem("Edit Column Settings", Nothing, New EventHandler(AddressOf Me.EditColumnSettings)))
    .Add(New ToolStripSeparator)

    With CM_SubMenu
    .Text = "Add Column Filter"
    .Name = "nmAddColumnFilter"
    '.Image =
    With .DropDownItems
    .Add("Equals ...", Nothing, New EventHandler(AddressOf Me.Equal))
    .Add("Does Not Equal ...", Nothing, New EventHandler(AddressOf Me.NotEqual))
    .Add("Begins With ...", Nothing, New EventHandler(AddressOf Me.BeginWith))
    .Add("Does Not Begin With ...", Nothing, New EventHandler(AddressOf Me.NotBeginWith))
    .Add("Ends With ...", Nothing, New EventHandler(AddressOf Me.EndWith))
    .Add("Does Not End With ...", Nothing, New EventHandler(AddressOf Me.NotEndWith))
    .Add("Contains ...", Nothing, New EventHandler(AddressOf Me.Contain))
    .Add("Does Not Contain ...", Nothing, New EventHandler(AddressOf Me.NotContains))
    .Add("Between ...", Nothing, New EventHandler(AddressOf Me.Between))
    .Add("Not Between ...", Nothing, New EventHandler(AddressOf Me.NotBetween))
    .Add("Is Blank", Nothing, New EventHandler(AddressOf Me.Blank))
    .Add("Is Not Blank", Nothing, New EventHandler(AddressOf Me.NotBlank))
    End With
    .DisplayStyle = ToolStripItemDisplayStyle.Text
    End With
    .AddRange(New ToolStripItem() {CM_SubMenu})

    .Add(New ToolStripControlHost(dgvUnique))
    End With

    ' disable Fill Column With button if the column is Read-Only
    CMS.Items("nmFillColumnWith").Enabled = Not Me.dgv.Columns(Me.ht.ColumnIndex).ReadOnly

    CMS.Show(Me.dgv, e.X, e.Y)

    End Sub
    End Class</pre>
    <br/>
    Anyone have a solution? I figure using the ToolStripControlHost class is the only way to do this, right?
    Thanks, <hr class="sig Ryan

    View the full article
     
    #1

Share This Page