我正在使用C#Visual Studio 2010开发用户控件 - 一种用于过滤datagridview的“快速查找”文本框 . 它应该适用于3种类型的datagridview数据源:DataTable,DataBinding和DataSet . 我的问题是从DataSet对象过滤DataTable,它显示在DataGridView上 .
可能有3个案例(标准WinForm应用程序的示例,其中包含DataGridView和TextBox) - 前2个工作正常,我遇到第3个问题:
1. datagridview.DataSource = dataTable : it works
所以我可以通过设置过滤:dataTable.DefaultView.RowFilter = "country LIKE '%s%'";
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("country", typeof(string));
dt.Rows.Add(new object[] { 1, "Belgium" });
dt.Rows.Add(new object[] { 2, "France" });
dt.Rows.Add(new object[] { 3, "Germany" });
dt.Rows.Add(new object[] { 4, "Spain" });
dt.Rows.Add(new object[] { 5, "Switzerland" });
dt.Rows.Add(new object[] { 6, "United Kingdom" });
dataGridView1.DataSource = dt;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("DataSource type BEFORE = " + dataGridView1.DataSource.GetType().ToString());
dt.DefaultView.RowFilter = string.Format("country LIKE '%{0}%'", textBox1.Text);
MessageBox.Show("DataSource type AFTER = " + dataGridView1.DataSource.GetType().ToString());
}
2. datagridview.DataSource = bindingSource: it works
所以我可以通过设置过滤:bindingSource.Filter = "country LIKE '%s%'";
DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("country", typeof(string));
dt.Rows.Add(new object[] { 1, "Belgium" });
dt.Rows.Add(new object[] { 2, "France" });
dt.Rows.Add(new object[] { 3, "Germany" });
dt.Rows.Add(new object[] { 4, "Spain" });
dt.Rows.Add(new object[] { 5, "Switzerland" });
dt.Rows.Add(new object[] { 6, "United Kingdom" });
bs.DataSource = dt;
dataGridView1.DataSource = bs;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("DataSource type BEFORE = " + dataGridView1.DataSource.GetType().ToString());
bs.Filter = string.Format("country LIKE '%{0}%'", textBox1.Text);
MessageBox.Show("DataSource type AFTER = " + dataGridView1.DataSource.GetType().ToString());
}
3. datagridview.DataSource = dataSource; datagridview.DataMember = "TableName": it doesn't work
当您使用设计器设计表时会发生这种情况:将DataSet从工具箱中放入表单,向其添加dataTable然后设置datagridview.DataSource = dataSource;和datagridview.DataMember = "TableName" .
下面的代码假装这些操作:
DataSet ds = new DataSet();
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("country", typeof(string));
dt.Rows.Add(new object[] { 1, "Belgium" });
dt.Rows.Add(new object[] { 2, "France" });
dt.Rows.Add(new object[] { 3, "Germany" });
dt.Rows.Add(new object[] { 4, "Spain" });
dt.Rows.Add(new object[] { 5, "Switzerland" });
dt.Rows.Add(new object[] { 6, "United Kingdom" });
ds.Tables.Add(dt);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = dt.TableName;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("DataSource type BEFORE = " + dataGridView1.DataSource.GetType().ToString());
//it is not working
ds.Tables[0].DefaultView.RowFilter = string.Format("country LIKE '%{0}%'", textBox1.Text);
MessageBox.Show("DataSource type AFTER = " + dataGridView1.DataSource.GetType().ToString());
}
如果你测试它 - 虽然数据表被过滤(ds.Tables [0] .DefaultView.Count更改),datagridview没有更新...我一直在寻找任何解决方案很长时间,但问题是 DataSource cannot change - 因为它想要它搞乱程序员的代码 .
我知道可能的解决方案是:
- 使用DataBinding从DataSet绑定DataTable并将其用作示例2:但是在代码编写期间由程序员决定,
- 将dataSource更改为BindingSource,dataGridView.DataSource = dataSet.Tables [0],或以编程方式更改为DefaultView:但是,它会更改DataSource . 所以解决方案:
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("DataSource type BEFORE = " + dataGridView1.DataSource.GetType().ToString(), ds.Tables[0].DefaultView.Count.ToString());
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = string.Format("country LIKE '%{0}%'", textBox1.Text);
dataGridView1.DataSource = dv;
MessageBox.Show("DataSource type AFTER = " + dataGridView1.DataSource.GetType().ToString(), ds.Tables[0].DefaultView.Count.ToString());
}
是不可接受的,正如你在MessageBox上看到的那样,dataSource正在改变......
我不想这样做,因为程序员可能会编写与此类似的代码:
private void textBox1_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("DataSource type BEFORE = " + dataGridView1.DataSource.GetType().ToString(), ds.Tables[0].DefaultView.Count.ToString());
DataSet dsTmp = (DataSet)(dataGridView1.DataSource); //<--- it is OK
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = string.Format("country LIKE '%{0}%'", textBox1.Text);
dataGridView1.DataSource = dv; //<--- here the source is changeing from DataSet to DataView
MessageBox.Show("DataSource type AFTER = " + dataGridView1.DataSource.GetType().ToString(), ds.Tables[0].DefaultView.Count.ToString());
dsTmp = (DataSet)(dataGridView1.DataSource); //<-- throws an exception: Unable to cast object DataView to DataSet
}
他可以做到这一点,因为他在设计器中使用DataSet和DataMember设计了DataGridView . 代码将被编译,但是,在使用过滤器后,它将抛出异常...
所以问题是:如何在DataSet中过滤DataTable并在DataGridView上显示结果而不将DataSource更改为另一个?为什么我可以直接从示例1过滤DataTable,而从DataSet过滤DataTable不起作用?在这种情况下,可能不是DataTable绑定到DataGridView?
请注意,我的问题来自设计问题,因此解决方案必须在示例3中工作 .
7 回答
我找到了解决这个问题的简单方法 . 在绑定datagridview时你刚刚完成了:
datagridview.DataSource = dataSetName.Tables["TableName"];
如果您的代码如下:
datagridview在过滤时永远不会再次加载数据 .
我只花了一个小时来解决类似的问题 . 对我来说,答案结果是令人尴尬的简单 .
我开发了一个通用语句来应用过滤器:
方括号允许列名称中的空格 .
此外,如果要在过滤器中包含多个值,可以为每个附加值添加以下行:
一种更简单的方法是横向数据,并使用
Visible
属性隐藏线条 .只是一个想法...它对我有用 .
您可以从数据源创建DataView对象 . 这将允许您在不直接修改数据源的情况下对数据进行筛选和排序 .
此外,请记住在设置数据源后调用
dataGridView1.DataBind();
.//“注释”过滤数据网格而不更改数据集,完美无缺 .
我对DataGridView中的自动搜索提出了更明确的建议
这是一个例子