C# 如何在我的应用程序中筛选DataGridView
我使用winforms DataGridView向导设置了两个网格。一个绑定到trips表,该表是我的事务表,另一个绑定到我的费用表。我了解一点linq2sql,现在我用fk(TripId)插入了我的费用表 我想做的是根据tripId过滤费用网格。我已经在检索当前所选旅行的TripId PK,以便完成该部分。考虑到我使用的是linq,但使用构建的向导绑定表,我不确定如何进行过滤 任何帮助都将不胜感激 编辑:我在下面蓝脚的帮助下走到了这一步。现在的问题是,当我进行过滤时,它只是清除网格,而不是基于pk进行过滤。这是完整的代码C# 如何在我的应用程序中筛选DataGridView,c#,winforms,linq-to-sql,c#-4.0,C#,Winforms,Linq To Sql,C# 4.0,我使用winforms DataGridView向导设置了两个网格。一个绑定到trips表,该表是我的事务表,另一个绑定到我的费用表。我了解一点linq2sql,现在我用fk(TripId)插入了我的费用表 我想做的是根据tripId过滤费用网格。我已经在检索当前所选旅行的TripId PK,以便完成该部分。考虑到我使用的是linq,但使用构建的向导绑定表,我不确定如何进行过滤 任何帮助都将不胜感激 编辑:我在下面蓝脚的帮助下走到了这一步。现在的问题是,当我进行过滤时,它只是清除网格,而不是基于
private void tripsBindingSource_PositionChanged(object sender, EventArgs e)
{
if (dgvTripGrid.CurrentRow != null)
{
//get selected row index
int index = this.dgvTripGrid.CurrentRow.Index;
//get pk of selected row using index
string cellValue = dgvTripGrid["pkTrips", index].Value.ToString();
//change pk string to int
int pKey = Int32.Parse(cellValue);
//int tripPrimKey = getPkRowTrips();
this.tripExpenseBindingSource.Filter = String.Format("tripNo = {0}",
pKey.ToString());
}
}
听起来您想根据第一个
datagridview
中的选择填充第二个datagridview
。这是一种方法:
- 在加载或搜索我的第一个
时,使用事件datagridview
然后填充第二个DataBindingComplete
基于在第一个datagridview
中选择的记录的iddatagridview
- 然后,如果第一个datagridview中的选择发生更改,我将使用
以重新填充第二个 网格BindingSource\u位置上的事件已更改
// this populates the grid.
private void SearchButton_Click(object sender, EventArgs e)
{
// your code to load your grid goes here
}
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
var drv = datagridview1bindingSource.Current as DataRowView;
if(drv != null)
// your method to load datagridview2 goes here if the selected row is not null
LoadDataGridView2();
}
private void LoadDataGridView2()
{
//populate datagridview2 using the selected row id from datagridview1
}
// finally when the position is changed on the datagridview1 binding source, then re-populate // the datagridview2
private void datagridview2BindingSource_PositionChanged(object sender, EventArgs e)
{
LoadDataGridView2();
}
这是基于第一个网格中的选择填充第二个网格的基本方法
编辑:
您的评论说您正在用所有费用填充datagridview
,因此要进行筛选,您需要对datagridview
使用BindingSource
上的filter
属性。Filter
属性允许您查看数据源的子集
示例来自:
private void填充数据视图和过滤器()
{
数据集set1=新数据集();
//用于填充数据集的一些xml数据。
弦乐=
"" +
"" +
“ColdplayX&;Y”+
“戴夫·马修斯趴在桌子上做梦”+
“戴夫·马修斯住在红岩”+
“娜塔莉·格丽丽”+
“如何拆除原子弹”+
"";
//阅读xml。
StringReader=新的StringReader(musicXml);
set1.ReadXml(reader);
//获取数据集中包含的表的DataView。
DataTableCollection tables=set1.tables;
DataView视图1=新的DataView(表[0]);
//创建DataGridView控件并将其添加到表单中。
DataGridView datagridview1=新建DataGridView();
datagridview1.AutoGenerateColumns=true;
this.Controls.Add(datagridview1);
//创建BindingSource并将其DataSource属性设置为
//数据视图。
BindingSource1=新的BindingSource();
source1.DataSource=view1;
//设置DataGridView的数据源。
datagridview1.DataSource=source1;
//筛选器字符串可以包括布尔表达式。
source1.Filter=“艺术家='Dave Matthews'或cd='Tigerly';
}
我使用这种类型的过滤器来显示基于帐户的数据。对于一个帐户,当用户放置帐号时,我有一个文本框,我使用TextChanged事件应用过滤器。然后我有一个按钮,用于从绑定源中删除过滤器
您可以使用第一个datagridview中的tripid将相同的内容应用于expense datagridview 听起来您想根据第一个
datagridview
中的选择填充第二个datagridview
。这是一种方法:
- 在加载或搜索我的第一个
时,使用事件datagridview
然后填充第二个DataBindingComplete
基于在第一个datagridview
中选择的记录的iddatagridview
- 然后,如果第一个datagridview中的选择发生更改,我将使用
以重新填充第二个 网格BindingSource\u位置上的事件已更改
// this populates the grid.
private void SearchButton_Click(object sender, EventArgs e)
{
// your code to load your grid goes here
}
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
var drv = datagridview1bindingSource.Current as DataRowView;
if(drv != null)
// your method to load datagridview2 goes here if the selected row is not null
LoadDataGridView2();
}
private void LoadDataGridView2()
{
//populate datagridview2 using the selected row id from datagridview1
}
// finally when the position is changed on the datagridview1 binding source, then re-populate // the datagridview2
private void datagridview2BindingSource_PositionChanged(object sender, EventArgs e)
{
LoadDataGridView2();
}
这是基于第一个网格中的选择填充第二个网格的基本方法
编辑:
您的评论说您正在用所有费用填充datagridview
,因此要进行筛选,您需要对datagridview
使用BindingSource
上的filter
属性。Filter
属性允许您查看数据源的子集
示例来自:
private void填充数据视图和过滤器()
{
数据集set1=新数据集();
//用于填充数据集的一些xml数据。
弦乐=
"" +
"" +
“ColdplayX&;Y”+
“戴夫·马修斯趴在桌子上做梦”+
“戴夫·马修斯住在红岩”+
“娜塔莉·格丽丽”+
“如何拆除原子弹”+
"";
//阅读xml。
StringReader=新的StringReader(musicXml);
set1.ReadXml(reader);
//获取数据集中包含的表的DataView。
DataTableCollection tables=set1.tables;
DataView视图1=新的DataView(表[0]);
//创建DataGridView控件并将其添加到表单中。
DataGridView datagridview1=新建DataGridView();
datagridview1.AutoGenerateColumns=true;
this.Controls.Add(datagridview1);
//创建BindingSource并将其DataSource属性设置为
//数据视图。
BindingSource1=新的BindingSource();
source1.DataSource=view1;
//设置DataGridView的数据源。
datagridview1.DataSource=source1;
//筛选器字符串可以包括布尔表达式。
source1.Filter=“艺术家='Dave Matthews'或cd='Tigerly';
}
我使用这种类型的过滤器来显示基于帐户的数据。对于一个帐户,当用户放置帐号时,我有一个文本框,我使用TextChanged事件应用过滤器。那我有一个