大家好,
我的代码运行得很好,但看起来很糟糕。其目的是搜索并显示我的数据库中符合搜索条件的工作人员。我的表单上有一个 DataGridView
,其中填充了从我的数据库绑定(bind)的工作人员数据。按Search
按钮后,我只想显示与TextBoxes/ComboBoxes
中的条件匹配的工作人员列表。如果它们为空,则会再次显示完整列表。
表单上的搜索功能:
代码:
private void btnSearch_Click(object sender, EventArgs e)
{
FillList();
List<Worker> list = new List<Worker>();
bool match = false;
foreach (var worker in workerBindingSource)
{
if (txtName.Text.Length > 0 && !((Worker)worker).Name.ToLowerInvariant().Contains(txtName.Text.ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (txtLastName.Text.Length > 0 && !((Worker)worker).LastName.ToLowerInvariant().Contains(txtLastName.Text.ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (txtOIB.Text.Length > 0 && !((Worker)worker).OIB.ToLowerInvariant().Contains(txtOIB.Text.ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (txtLocation.Text.Length > 0 && !((Worker)worker).Location.ToLowerInvariant().Contains(txtLocation.Text.ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (txtAddress.Text.Length > 0 && !((Worker)worker).Address.ToLowerInvariant().Contains(txtAddress.Text.ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (txtPhoneNumber.Text.Length > 0 && !((Worker)worker).PhoneNumber.ToLowerInvariant().Contains(txtPhoneNumber.Text.ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (!cboProfessionalQualification.SelectedValue.ToString().Equals("Empty") && !((Worker)worker).ProfessionalQualification.ToString().ToLowerInvariant().Contains(cboProfessionalQualification.SelectedValue.ToString().ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (!cboDegree.SelectedValue.ToString().Equals("Empty") && !((Worker)worker).Degree.ToString().ToLowerInvariant().Equals(cboDegree.SelectedValue.ToString().ToLowerInvariant()))
{
match = false;
continue;
}
else
match = true;
if (match)
{
list.Add(WorkerDAO.ReadWorker(((Worker)worker).ID));
}
}
SortableBindingList<Worker> sortableList = new SortableBindingList<Worker>(list);
workerBindingSource.DataSource = sortableList;
}
代码中带有“txt”前缀的所有内容都是TextBox
,而带有“cbo”前缀的所有内容都是ComboBox
。 ComboBoxes
包含用户可以选择的某些枚举。如果ComboBox
显示枚举值“Empty”,则意味着用户没有选择任何内容(与字符串的零长度值相同)。 SortableBindingList
仅用于列标题单击时的排序目的,在这种情况下并不重要。仅当 ReadWorker 方法与所有搜索值匹配时才会返回工作人员,然后将其添加到我在 DataGridView 上显示的列表中。 FillList
方法正在将数据库中的整个工作人员列表设置为 DataBindingSource
。
ReadWorker方法:
public static Worker ReadWorker(int workerID)
{
var worker = ReadEverything().Where(x => x.ID == workerID).FirstOrDefault();
return worker;
}
方法ReadEverything
返回数据库中所有工作人员的列表。
我正在寻找一个 LINQ 解决方案(或任何其他更好的解决方案)来大幅减少我的代码并使其更具可读性,但由于我对编程和 LINQ 还很陌生,所以我自己无法弄清楚。所以我想知道你们是否可以帮助我一点,或者至少为我指明正确的方向。
提前致谢!
最佳答案
如果你的datagridview已经拥有了数据库中的所有数据,我建议你不要再去数据库搜索,因为你已经拥有了所有的数据。只需在内存中过滤并显示即可。
如果由于某种原因你需要访问数据库,没有理由从数据库获取所有数据并在客户端过滤它,只需将“workerId”传递给存储过程,让SP添加适当的where条件并返回过滤后的数据。
使用绑定(bind)源实现内存过滤:
bindingSource.Filter = "columnname = 'value'";
private void button1_Click(object sender, EventArgs e)
{
string searchValue = textBox1.Text;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
bindingSource.Filter = string.Format("{0} = '{1}'","YourColumnName", searchValue );
//here you can do selection if you need
}
To remove filter use the following
bindingSource.RemoveFilter();
//or
bindingSource.Filter = null;
最初作为答案发布here .
关于c# - 优化绑定(bind)DataGridView搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23524008/