【问题标题】:Data confusion - Selecting data in one DataGridView based on selection in another数据混淆 - 根据另一个 DataGridView 中的选择来选择一个 DataGridView 中的数据
【发布时间】:2009-10-05 08:54:18
【问题描述】:

这可能不是做我想做的最好的方法,但我想不出其他可以尝试的方法......

注意:我使用的是 Visual Basic.NET

我的表单有 2 个 DataGridView 控件。其中一个绑定到 DataSet,另一个不可见 - 至少在用户选择第一个网格中的 uniqueidentifier 单元格之前是不可见的。

当用户进行此选择时,第二个网格将变得可见,并显示来自另一个网格的行,其 id 与在第一个网格中选择的行相同。

所以,基本上,我想根据用户在另一个网格中的选择在一个网格中动态显示数据。

到目前为止,我的代码看起来像这样......

Private Sub RulesGrid_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles RulesGrid.CellClick
    Try
        FlagsGrid.Visible = True
        ''// MsgBox(RulesGrid.CurrentCell.Value.ToString())
        Dim sql As String = "SELECT * FROM ctblMKA_Status_Flags " + _
            "WHERE intStatusID = '" & RulesGrid.CurrentCell.Value & "'"
        DSFlags = GetDS(sql)
        DSFlags.DataSetName = "FlagsDataSet"
        FlagsGrid.DataSource = DSFlags
        ProgressBar.Visible = False
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

我觉得我在这里遗漏了一些东西......有人有什么想法吗?

【问题讨论】:

    标签: vb.net winforms datagridview


    【解决方案1】:

    为第二个 DataGrid 使用单独的 DataView。

    【讨论】:

      【解决方案2】:

      如果您使用的是 DataTable,请分配一个 CurrencyManager,然后在您的 CurrencyManager 变量上添加 PositionChanged。

      类级变量:

      CurrencyManager _cmShoe;
      

      将数据加载到数据表后:

      // make a currency(current) manager on DataTable variable
      _cmShoe = this.BindingContext[_dtShoe] as CurrencyManager;
      
      // add event on the CurrencyManager variable
      _cmShoe.PositionChanged += cmShoe_PositionChanged;
      

      事件:

      void cmShoe_PositionChanged(object sender, EventArgs e)
      {
          Guid shoeGuid = (Guid)((cmShoe.Current as DataRowView)["shoe_id"]);
      
          // _dtShoeMaterial is the table of the second datagridview
          _dtShoeMaterial.DefaultView.RowFilter = "shoe_id = '" + shoeGuid.ToString() + "'";
      }
      

      如果您使用的是 BindingSource(而不是 DataTable),只需在 BindingSource 变量上添加一个 PositionChanged 事件,并使用此事件:

      void cmShoe_PositionChanged(object sender, EventArgs e)
      {
          Guid shoeGuid = (Guid)((bdsShoe.Current as DataRowView)["shoe_id"]);
      
          // bdsShoeMaterial is the table of the second datagridview
          bdsShoeMaterial.Filter = "shoe_id = '" + shoeGuid.ToString() + "'";        
      }
      

      【讨论】:

      • 我会尝试不同的事件处理程序,但恐怕我无法从代码中得到太多。我不做 C#...我知道它更好,但每个人都在做,我觉得自己像一个工具或什么的...
      • 把这段代码转换成VB很容易。您可以使用工具来转换代码,例如:developerfusion.com/tools/convert/csharp-to-vb
      • 唯一重要的区别是在 C# 和 VB 中处理事件的方式。此行:_cmShoe.PositionChanged += cmShoe_PositionChanged,必须转换为:AddHandler _cmShoe.PositionChanged, AddressOf cmShoe_PositionChanged
      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 2011-08-17
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多