我正在 C # Visual Studio 2010中开发用户控件——一种用于过滤 datagridview 的“快速查找”文本框。它应该适用于3种类型的 datagridview 数据源: DataTable、 DataBinding 和 DataSet。 我的问题是从 DataSet 对象中筛选 DataTable,该对象显示在 DataGridView 上。
可能有3种情况(例如,带有 DataGridView 和 TextBox 的标准 WinForm 应用程序)——前2种工作正常,第3种有问题:
1. datagridview.DataSource = dataTable : it works
所以我可以通过设置: dataTable.DefaultView
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: 它可以工作
所以我可以通过设置: bindingSource 进行过滤
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”: 它不工作
使用设计器设计表时会发生这种情况: 将工具箱中的 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 不会更新..。 I've been looking for a long time for any solution, but the problem is that DataSource 无法更改 - as it's additional control, I don't want it to mess up with programmer's code.
我知道可能的解决办法是:
- 使用 DataBinding 从 DataSet 绑定 DataTable 并将其作为示例2使用: 但在编写代码期间取决于程序员,
- 将 dataSource 更改为 BindingSource,dataGridView。DataSource = dataSet.表[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。 Code will be compiled, however, after using a filter, it will throw an exception...
所以问题是: 如何在 DataSet 中过滤 DataTable 并在 DataGridView 上显示结果而不将 DataSource 更改为另一个?为什么我可以直接从示例1过滤 DataTable,而从 DataSet 过滤 DataTable 不起作用? 在这种情况下,也许它不是绑定到 DataGridView 的 DataTable?
请注意,我的问题需要从设计问题,所以解决方案必须工作在例子3。