我想用3个文本框过滤DataGridView,我正确地得到了值,但它只适用于第一列('Surname'),当我输入'Name'或'Phone'时它不会过滤:
protected void btnRic_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtSurname.Text) || !string.IsNullOrEmpty(txtName.Text))
{
isPhoneNumber = false;
if (txtCell.Value.Length >= 8)
{
string phoneNumber = txtCell.Value;
isPhoneNumber = true;
}
DataTable dt = ViewState["dTableGrid"] as DataTable;
DataView dv = dt.DefaultView;
if (!isPhoneNumber)
{
dv.RowFilter = string.Format("Surname LIKE '{0}%' OR Surname LIKE '% {0}%' OR Name LIKE ' {1}%' OR Name LIKE '% {1}%'", txtSurname.Text, txtName.Text);
}
else
{
dv.RowFilter = string.Format("Surname LIKE ' {0}%' OR Surname LIKE '% {0}%' OR Name LIKE ' {1}%' OR Name LIKE '% {1}%' OR Phone LIKE ' {2}%' OR Phone LIKE '% {2}%'" , txtSurname.Text, txtName.Text, txtCell.Value);
}
Grid.DataSource = dv.ToTable();
Grid.DataBind();
答案 0 :(得分:2)
if (!isPhoneNumber)
{
dv.RowFilter = string.Format("Surname LIKE '{0}%' OR Surname LIKE '%{0}%' OR Name LIKE '{1}%' OR Name LIKE '%{1}%'", txtSurname.Text, txtName.Text);
}
else
{
dv.RowFilter = string.Format("Surname LIKE '{0}%' OR Surname LIKE '%{0}%' OR Name LIKE '{1}%' OR Name LIKE '%{1}%' OR Phone LIKE '{2}%' OR Phone LIKE '%{2}%'" , txtSurname.Text, txtName.Text, txtCell.Value);
}
<强>更新强>
检查'% {0}%'
之间的空格是否导致问题。