这可能不是我想做的最好的方式,但我想不出别的事情可以尝试......
注意:我正在使用Visual Basic.NET
我的表单有2个DataGridView控件。其中一个绑定到DataSet,另一个不可见 - 至少在用户选择第一个网格中的uniqueidentifier单元格之前。
当用户进行此选择时,第二个网格将变为可见,并显示另一个网格的行,其ID与第一个网格中选择的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
我觉得我在这里遗漏了什么......有什么想法吗?
答案 0 :(得分:0)
为第二个DataGrid使用单独的DataView。
答案 1 :(得分:0)
如果您使用的是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() + "'";
}