我有这个代码并且它工作得很好,但是一旦我想写更多LIKE运算符我的整个gridview变化。
示例:我想:
cmd.Parameters.AddWithValue("@Number", txtSearch.Text.Trim());
cmd.Parameters.AddWithValue("@Place ", txtSearch.Text.Trim());
protected void btnFilter_Click(object sender, EventArgs e)
{
string name = Session["user"].ToString();
string constr = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = @"SELECT * From Product
Inner Join User
ON product.user = user.id
Where user.login = @User
AND Status = @Status
AND (Number LIKE '%' + @Number + '%')";
cmd.Parameters.AddWithValue("@User", name);
cmd.Parameters.AddWithValue("@Status", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@Number", txtSearch.Text.Trim());
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
ad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
我的Gridview忽略了会话,在登录时,您可以看到每个用户的数据。
登录是用户名。
但如果我保持这样,那就完美了:
<asp:GridView ID="GridView1" DataKeyNames="Number" AllowSorting="true" OnRowDataBound="RowDataBound" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="false" />
<asp:BoundField DataField="Number" HeaderText="Nummer" SortExpression="Number" />
<asp:BoundField DataField="Login" HeaderText="Datum" SortExpression="Login" />
<asp:BoundField DataField="Date" HeaderText="Datum" SortExpression="Date" />
</Columns>
</asp:GridView>
var $inputs = $(".searchskill");
for (let i = 0, iLen = $inputs.length; i < iLen; i++) {
var $elem = $($inputs[i]);
if($elem.val()===""){
$elem.parent("div").addClass("border-red alert-danger");
$elem.focus();
}
}
答案 0 :(得分:0)
您需要将OR
分组到正确的括号中,否则会针对所有其他过滤器OR
进行分组。它是基本的布尔代数:
A和B以及C或D将在D为真时返回true
只有当A,B和(C或D)为真时,A和B以及(C或D)才会返回true。
更改
AND (Number LIKE '%' + @Number + '%') OR (Place LIKE '%' + @Place + '%')
到
AND ((Number LIKE '%' + @Number + '%') OR (Place LIKE '%' + @Place + '%'))