C#Datagrid的WHERE子句的文本框

时间:2012-06-18 20:42:17

标签: c# sql datagrid

(VS 2010 C#Website)我有TextBox1来过滤将在Datagrid中显示的SQL数据库中的FirstNames,但我无法找出将用户输入从“TextBox1.Text”链接到的正确方法Datagrid筛选的WHERE子句。我试过了:

SelectCommand="SELECT * FROM [TestName] WHERE ([TestFirstName] LIKE '%' + TextBox1.Text + '%')"

4 个答案:

答案 0 :(得分:1)

SelectCommand="SELECT * FROM [TestName] WHERE ([TestFirstName] LIKE '%' + @TestFirstName2 + '%')">
    <SelectParameters>
        <asp:ControlParameter ControlID="TextBox1" Name="TestFirstName2" 
            PropertyName="Text" Type="String" />
    </SelectParameters>

答案 1 :(得分:0)

试试这个:

SelectCommand="SELECT * FROM [TestName] WHERE ([TestFirstName] LIKE '%" + TextBox1.Text + "%')" 

注意+ TextBox1.Text +周围的引号。上面的示例似乎是在查找文字字符串TextBox1.Text,而不是TextBox1.Text的实际值。

答案 2 :(得分:0)

SelectCommand = "SELECT * FROM [TestName] WHERE ([TestFirstName] LIKE '% " + TextBox1.Text + "%')";

答案 3 :(得分:0)

如果使用SqlDataSource或ObjectDataSource将数据绑定到数据网格,则可以简单地将WHERE子句放入您正在使用的DataSource的“FilterExpression”属性中。

protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            string filterstr = "";

            if (this.tbSearchEmpID.Text.Trim() != String.Empty)
            {
                filterstr += "EmpId LIKE '%" + this.tbSearchEmpID.Text.Trim() + "%'";
            }

            this.ObjectDataSource1.FilterExpression = filterstr;
        }