如何获取子数据网格值

时间:2014-03-14 11:43:11

标签: vb.net datagridview parent-child

如何在子弹出窗口DataGridView中保存值?

我在vb.net中有父数据网格视图和子DataGridView

考虑我有2列和2行。

当我点击第一列时,我会在这个单元格附近找到孩子DataGridView。子DataGridView也有2行2列,我可以在其中输入值。

当我点击第二列时,我会在此单元格附近再生一个新的孩子DataGridView

现在如果我回到第一列,我输入的值就会丢失。如何在弹出子窗口中保存输入的值?

这是我的代码:

sub cell_click Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location Dim _pointGrid As Point = DgV.Location Dim _pointLocation As Point _pointLocation.X = _pointCell.X 'width _pointLocation.Y = _pointCell.Y 'height SelectionInGrid() mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y))) end sub

Public SelectionInGrid() Dim t1,t2 As New DataGridViewTextBoxColumn() Dim gv As New DataGridView

    gv.Columns.Add(t1)
    gv.Columns.Add(t2)
    gv.Columns(0).HeaderText = "Employee"
    gv.Columns(1).HeaderText = "Currency"

    gv.Width = t1.Width + t2.Width

    Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
    mControlHost.Padding = Padding.Empty
    mControlHost.AutoSize = False

    mPopup = New ToolStripDropDown()
    mPopup.Padding = Padding.Empty
    mPopup.Items.Add(mControlHost)
End

sub cell_click
            Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
            Dim _pointGrid As Point = DgV.Location
            Dim _pointLocation As Point
           _pointLocation.X = _pointCell.X 'width
           _pointLocation.Y = _pointCell.Y  'height
            SelectionInGrid()
            mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y)))
end sub

Public SelectionInGrid()
    Dim t1,t2 As New DataGridViewTextBoxColumn()
    Dim gv As New DataGridView

    gv.Columns.Add(t1)
    gv.Columns.Add(t2)
    gv.Columns(0).HeaderText = "Employee"
    gv.Columns(1).HeaderText = "Currency"

    gv.Width = t1.Width + t2.Width

    Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
    mControlHost.Padding = Padding.Empty
    mControlHost.AutoSize = False

    mPopup = New ToolStripDropDown()
    mPopup.Padding = Padding.Empty
    mPopup.Items.Add(mControlHost)
End 

1 个答案:

答案 0 :(得分:0)

问题在于SelectionInGrid Sub,每次单击一个单元格时都会调用它。在这个函数中你有这些行:

Dim gv As New DataGridView
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mPopup = New ToolStripDropDown()

这意味着每次cell_click运行新的DataGridView时,都会创建一个新的ToolStripControlHost和一个新的ToolStripDropDown。要解决此问题,您需要跟踪不同的ToolStripDropDowns。例如,使用字典:

Private PopUps As New Dictionary(Of String, ToolStripDropDown)    
sub cell_click
Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
Dim _pointGrid As Point = DgV.Location
Dim _pointLocation As Point
_pointLocation.X = _pointCell.X 'width
_pointLocation.Y = _pointCell.Y  'height     
If PopUps.ContainsKey(<Parent Selected cell identifyer>) Then
   mPopup = PopUps(<Parent Selected cell identifyer>)
Else
    SelectionInGrid()
    PopUps.Add(<Parent Selected cell identifyer>,mPopup)
End If
mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y)))
end sub

我相信这应该有效。 &#34;父选择的细胞识别器&#34;必须是单击的父DGV行/单元格中的唯一内容。