dataGridView. Фильтрация по нескольким условиям
Фильтрация по кнопке:
private void button3_Click(object sender, EventArgs e) { BindingSource bds = new BindingSource(); List<string> filterParts = new List<string>(); if (tB_w_ID.Text != "") filterParts.Add("W_ID LIKE '%" + tB_w_ID.Text + "%'"); if (tB_w_Name.Text != "") filterParts.Add("W_NAME LIKE '%" + tB_w_Name.Text + "%'"); if (tB_w_Surname.Text != "") filterParts.Add("W_SURNAME LIKE '%" + tB_w_Surname.Text + "%'"); if (tB_w_Patronymic.Text != "") filterParts.Add("W_Patronymic LIKE '%" + tB_w_Patronymic.Text + "%'");
string filter = string.Join(" AND ", filterParts); workersBindingSource.Filter = filter;
}
Фильтрация в момент ввода текста в TextBox:
private void tB_w_Patronymic_TextChanged(object sender, EventArgs e)
{
UpdateDgv();
} private void UpdateDgv()
{
string filter = GetFilter(); //if (filter != string.Empty)
// this.Text = filter;
//else this.Text = "All records"; workersBindingSource.Filter = filter;
} private string GetFilter()
{
string filter = string.Empty; if (tB_w_ID.Text != string.Empty)
filter = string.Format("CONVERT(W_ID,'System.String') like '{0}%'", tB_w_ID.Text); if (tB_w_Name.Text != string.Empty)
{
if (tB_w_ID.Text != string.Empty)
filter += " and ";
filter += string.Format("W_NAME like '{0}%'", tB_w_Name.Text);
} if (tB_w_Surname.Text != string.Empty)
{
if (filter != string.Empty)
filter += " and ";
filter += string.Format("W_SURNAME like '{0}%'", tB_w_Surname.Text);
} if (tB_w_Patronymic.Text != string.Empty)
{
if (filter != string.Empty)
filter += " and ";
filter += string.Format("W_Patronymic like '{0}%'", tB_w_Patronymic.Text);
Комментарии
Отправить комментарий