Winforms 编辑和过滤datagridview时需要设计建议

Winforms 编辑和过滤datagridview时需要设计建议,winforms,datagridview,datatable,Winforms,Datagridview,Datatable,我有一个设计问题,涉及DataGridView和DataTable,由WinForms应用程序中的BindingSource绑定。DataTable由sql server表填充 用户可以在DataGrid中添加新行、删除行和编辑行。 完成后,他单击一个按钮,该按钮将在DataTable中启动一个循环,根据他在DataGridView中所做的操作,该循环将编辑一些表,插入一些表,并从一些表中删除 这一切都没有问题,而且效果很好。 但是现在我必须让用户也过滤数据,这样DataGridView将显示更

我有一个设计问题,涉及DataGridView和DataTable,由WinForms应用程序中的BindingSource绑定。DataTable由sql server表填充

用户可以在DataGrid中添加新行、删除行和编辑行。
完成后,他单击一个按钮,该按钮将在DataTable中启动一个循环,根据他在DataGridView中所做的操作,该循环将编辑一些表,插入一些表,并从一些表中删除

这一切都没有问题,而且效果很好。 但是现在我必须让用户也过滤数据,这样DataGridView将显示更多或更少的记录

这里的问题是,当用户添加新行、删除几行、更改几行,然后应用可以筛选出一个或多个记录的筛选器时,按钮中的循环仍应看到这些记录以对其进行处理


处理此问题的最佳设计是什么?

过滤器不应影响循环。例如,在下面的代码中,我从一个
DataTable
设置了
DataGridView.DataSource
,该表具有一个应用的过滤器并在表中循环,打印值:

DataTable dt = new DataTable();

dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Quantity", typeof(int));

dt.Rows.Add("Foo", 1);
dt.Rows.Add("Bar", 2);
dt.Rows.Add("Baz", 3);

string filterField = "Name";
string filterText = "B";
dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", filterField, filterText);

this.dataGridView1.DataSource = dt;

foreach (DataRow row in dt.Rows)
{
    Console.WriteLine("{0}", row.ItemArray[0]);
}
使用该过滤器,
DataGridView
仅显示选择项。但是在
DataTable
行中循环仍然会打印每个条目

因此,在处理绑定(如
BindingSource
)时,在
DataGridView
已经获得源代码后,您可以这样更改过滤器以具有动态搜索选项:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    BindingSource bs = this.dataGridView1.DataSource as BindingSource;
    DataTable dt = bs.DataSource as DataTable;
    dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '{1}%'", "Name", this.textBox1.Text);
}

过滤器不应该影响循环。例如,在下面的代码中,我从一个
DataTable
设置了
DataGridView.DataSource
,该表具有一个应用的过滤器并在表中循环,打印值:

DataTable dt = new DataTable();

dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Quantity", typeof(int));

dt.Rows.Add("Foo", 1);
dt.Rows.Add("Bar", 2);
dt.Rows.Add("Baz", 3);

string filterField = "Name";
string filterText = "B";
dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", filterField, filterText);

this.dataGridView1.DataSource = dt;

foreach (DataRow row in dt.Rows)
{
    Console.WriteLine("{0}", row.ItemArray[0]);
}
使用该过滤器,
DataGridView
仅显示选择项。但是在
DataTable
行中循环仍然会打印每个条目

因此,在处理绑定(如
BindingSource
)时,在
DataGridView
已经获得源代码后,您可以这样更改过滤器以具有动态搜索选项:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    BindingSource bs = this.dataGridView1.DataSource as BindingSource;
    DataTable dt = bs.DataSource as DataTable;
    dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '{1}%'", "Name", this.textBox1.Text);
}

过滤器不应该影响循环。例如,在下面的代码中,我从一个
DataTable
设置了
DataGridView.DataSource
,该表具有一个应用的过滤器并在表中循环,打印值:

DataTable dt = new DataTable();

dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Quantity", typeof(int));

dt.Rows.Add("Foo", 1);
dt.Rows.Add("Bar", 2);
dt.Rows.Add("Baz", 3);

string filterField = "Name";
string filterText = "B";
dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", filterField, filterText);

this.dataGridView1.DataSource = dt;

foreach (DataRow row in dt.Rows)
{
    Console.WriteLine("{0}", row.ItemArray[0]);
}
使用该过滤器,
DataGridView
仅显示选择项。但是在
DataTable
行中循环仍然会打印每个条目

因此,在处理绑定(如
BindingSource
)时,在
DataGridView
已经获得源代码后,您可以这样更改过滤器以具有动态搜索选项:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    BindingSource bs = this.dataGridView1.DataSource as BindingSource;
    DataTable dt = bs.DataSource as DataTable;
    dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '{1}%'", "Name", this.textBox1.Text);
}

过滤器不应该影响循环。例如,在下面的代码中,我从一个
DataTable
设置了
DataGridView.DataSource
,该表具有一个应用的过滤器并在表中循环,打印值:

DataTable dt = new DataTable();

dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Quantity", typeof(int));

dt.Rows.Add("Foo", 1);
dt.Rows.Add("Bar", 2);
dt.Rows.Add("Baz", 3);

string filterField = "Name";
string filterText = "B";
dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '%{1}%'", filterField, filterText);

this.dataGridView1.DataSource = dt;

foreach (DataRow row in dt.Rows)
{
    Console.WriteLine("{0}", row.ItemArray[0]);
}
使用该过滤器,
DataGridView
仅显示选择项。但是在
DataTable
行中循环仍然会打印每个条目

因此,在处理绑定(如
BindingSource
)时,在
DataGridView
已经获得源代码后,您可以这样更改过滤器以具有动态搜索选项:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    BindingSource bs = this.dataGridView1.DataSource as BindingSource;
    DataTable dt = bs.DataSource as DataTable;
    dt.DefaultView.RowFilter = string.Format("[{0}] LIKE '{1}%'", "Name", this.textBox1.Text);
}

我刚刚找到了一种更好的方法,可以从以下人员发布的问题(是的,这是一个问题)中过滤
DataGridView

这是问题中最有用的部分

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, "Swiss" });
    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());
}

我刚刚找到了一种更好的方法,可以从以下人员发布的问题(是的,这是一个问题)中过滤
DataGridView

这是问题中最有用的部分

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, "Swiss" });
    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());
}

我刚刚找到了一种更好的方法,可以从以下人员发布的问题(是的,这是一个问题)中过滤
DataGridView

这是问题中最有用的部分

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, "Swiss" });
    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());
}

我刚刚找到了一种更好的方法,可以从以下人员发布的问题(是的,这是一个问题)中过滤
DataGridView

这是问题中最有用的部分

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, "Swiss" });
    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());
}

DefaultView.RowFilter看起来很有前途,明天将首先尝试。我的DataTable是使用BindingSource绑定到DataGrid的,所以我想我只需要设置这个属性,BindingSource就会注意DataGridView是否显示过滤后的结果集?@GuidoG Correct。在
DataTable
上设置过滤器,然后设置
bindingSource.DataSource=DataTable
dataGridView.DataSource=bindingSource
仍将在dgv中正确显示筛选列表,而在该数据表中迭代时,
将显示未筛选列表。DefaultView.RowFilter看起来很有希望,明天将首先尝试。我的DataTable是使用BindingSource绑定到DataGrid的,所以我想我只需要设置这个属性,BindingSource就会注意DataGridView是否显示过滤后的结果集?@GuidoG Correct。在
DataTable
上设置过滤器,然后设置
bindingSource.DataSource=DataTable
dataGridView.DataSource=bindingSource
仍将在dgv中正确显示筛选列表,而在该数据表中迭代时,
将显示未筛选列表。DefaultView.RowFilter看起来很有希望,明天将首先尝试。我的DataTable是使用BindingSource绑定到DataGrid的,所以我想我只需要设置这个属性,BindingSource就会注意DataGridView是否显示过滤后的结果集?@GuidoG Correct。在
DataTable
上设置过滤器,然后设置
bindingSource.DataSource=DataTable
dataGridView.DataSource=bindingSource
仍将在dgv中正确显示已过滤列表,而在该
数据表中迭代时,
将显示未过滤的lis