设计建议需要编辑和过滤datagridview [英] Design advise needed on editing and filtering a datagridview

查看:101
本文介绍了设计建议需要编辑和过滤datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



用户可以在DataGrid中添加新行,删除行和编辑行。

当他完成后,他点击一个按钮,该按钮将通过DataTable启动循环,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(dt.Rows中的DataRow行)
{
Console.WriteLine({0},row.ItemArray [0]);
}

使用过滤器, DataGridView 仅显示选择条目。但是循环使用 DataTable 行仍会打印每个条目。





因此,在处理诸如 BindingSource 之类的绑定时,在 DataGridView 已经提供,您可以像这样更改过滤器,使其具有动态搜索选项:

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


I have a design question that involves DataGridView and DataTable, bound by a BindingSource in a WinForms application. The DataTable is populated by a sql server table.

The user can add new rows in the DataGrid, delete rows and edit rows.
When he is done he clicks on a button which will start a loop through the DataTable which will edit some tables, insert into some table and delete from some tables depending on what he did in the DataGridView.

This is all no problem and it works fine. But now I have to let the user also filter the data, so the DataGridView will show more or less records.

The problem here is that when the user adds a new row, deletes a few rows, changes a few rows, and than applies a filter that could filter out one or more of these records, the loop in the button should still see these records to process them.

What is the best design for handling this ?

解决方案

The filter shouldn't affect the loop. For example, in the following code I set the DataGridView.DataSource from a DataTable that has an applied filter and loop through the table, printing values:

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]);
}

With the filter, the DataGridView displays only select entries. But looping through the DataTable rows still prints every entry.

Therefore, when handling binding such as a BindingSource, after the DataGridView is already sourced, you might change your filter like so to have a dynamic searching option:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆