如何在gridview中对搜索到的记录进行排序

时间:2013-09-23 06:01:23

标签: c# asp.net gridview

我通过实现以下功能成功地对所有记录进行了排序。

private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;

        return (SortDirection)ViewState["sortDirection"];
    }
    set { ViewState["sortDirection"] = value; }
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;

    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView(sortExpression, DESCENDING);
    }
    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView(sortExpression, ASCENDING);
    }
}

private void SortGridView(string sortExpression, string direction)
{
    //  You can cache the DataTable for improving performance
    DataTable dt = GetData().Tables[0];

    DataView dv = new DataView(dt);
    dv.Sort = sortExpression + direction;

    GridView1.DataSource = dv;
    GridView1.DataBind();
}
public DataSet GetData()
{
    SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            String sql = "select * from dbo.Documents1";
            cmd.Connection = con;
            cmd.CommandText = sql;

            con.Open();

            DataSet ds = new DataSet();

            using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
            {
                adp.Fill(ds);

            }

            return ds;
        }
    }

}

我在搜索记录时遇到问题。我申请的代码如下:

protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression = e.SortExpression;

    if (GridViewSortDirection == SortDirection.Ascending)
    {
        GridViewSortDirection = SortDirection.Descending;
        SortGridView1(sortExpression, DESCENDING);
    }
    else
    {
        GridViewSortDirection = SortDirection.Ascending;
        SortGridView1(sortExpression, ASCENDING);
    }
}

private void SortGridView1(string sortExpression, string direction)
{

    DataTable dt = SearchTable().Tables[0];

    DataView dv = new DataView(dt);
    dv.Sort = sortExpression + direction;

    GridView2.DataSource = dv;
    GridView2.DataBind();

}

public DataSet SearchTable()
    {

        string sql1 = "SELECT * from dbo.Documents1";

        bool flag = false;

        if (!txtRef.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Ref LIKE N'%" + txtRef.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Ref LIKE N'%" + txtRef.Text + "%'";
            }
        }

        if (!txtSubject.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Subject LIKE N'%" + txtSubject.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Subject LIKE N'%" + txtSubject.Text + "%'";
            }
        }
        if (!ddlSource.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Src =N'" + ddlSource.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Src =N'" + ddlSource.Text + "'";
            }
        }
        if (!ddlDestination.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Dst=N'" + ddlDestination.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Dst =N'" + ddlDestination.Text + "'";
            }
        }

        if (!ddlMedium.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Medium =N'" + ddlMedium.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Medium =N'" + ddlMedium.Text + "'";
            }
        }
        if (!txtDatePrinted.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Date_Printed =N'" + txtDatePrinted.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Date_Printed =N'" + txtDatePrinted.Text + "'";
            }
        }


        if (!txtDateReceived.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Date_Received =N'" + txtDateReceived.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Date_Received =N'" + txtDateReceived.Text + "'";
            }
        }
        if (!ddlDocumentType.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Document_Type =N'" + ddlDocumentType.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Document_Type =N'" + ddlDocumentType.Text + "'";
            }
        }
        if (!txtDueDate.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Due_Date = N'" + txtDueDate.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Due_Date  =N'" + txtDueDate.Text + "'";
            }
        }
        if (!txtActualDate.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Actual_Date= N'" + txtActualDate.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Actual_Date=N'" + txtActualDate.Text + "'";
            }
        }

        if (!txtContent.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Content=N'" + txtContent.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Content=N'" + txtContent.Text + "'";
            }
        }
        if (!txtTag.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Tag =N'" + txtTag.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Tag =N'" + txtTag.Text + "'";
            }
        }
        if (!txtIssue.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Issue_No = N'" + txtIssue.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Issue_No = N'" + txtIssue.Text + "'";
            }
        }
        if (!txtNotes.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Notes = N'" + txtNotes.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Notes  = N'" + txtNotes.Text + "'";
            }
        }
        if (!ddlAssignedTo.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Assigned_To = N'" + ddlAssignedTo.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Assigned_To  = N'" + ddlAssignedTo.Text + "'";
            }
        }
        if (!txtReplyRef.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Reply_Ref LIKE N'%" + txtReplyRef.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Reply_Ref  LIKE N'%" + txtReplyRef.Text + "%'";
            }
        }
        if (!ddlPriority.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Priority = N'" + ddlPriority.Text + "'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Priority  = N'" + ddlPriority.Text + "'";
            }
        }
        if (!ddlStatus.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Status LIKE N'%" + ddlStatus.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Status LIKE N'%" + ddlStatus.Text + "%'";
            }
        }
        if (!ddlResponse.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Response LIKE N'%" + ddlResponse.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Response LIKE N'%" + ddlResponse.Text + "%'";
            }
        }
        if (!txtPhysicalFileNo.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Physical_File_No LIKE N'%" + txtPhysicalFileNo.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Physical_File_No  LIKE N'%" + txtPhysicalFileNo.Text + "%'";
            }
        }
        if (!txtPhysicalRackLocation.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " where Physical_Rack_Location LIKE N'%" + txtPhysicalRackLocation.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Physical_Rack_Location  LIKE N'%" + txtPhysicalRackLocation.Text + "%'";
            }
        }

        using (SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.Connection = con;
                cmd.CommandText = sql1 + ";";
                //cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                //dataset object to get all select statement results
                DataSet ds = new DataSet();

                //sql dataadoptor to fill dataset
                using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                {
                    adp.Fill(ds);
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

                return ds;

            }
        }
    }

上面的代码是在Gridview2中对搜索到的记录进行排序,但是当它绑定记录时,它会绑定数据库表中的所有记录...而我只需要对搜索到的记录进行绑定和显示。我不明白我哪里出错了。任何帮助都感激不尽。提前致谢。

1 个答案:

答案 0 :(得分:0)

请像这样更改您的searchTable函数查询。

      string sql1 = "SELECT * from dbo.Documents1 where 1=1";

        bool flag = false;

        if (!txtRef.Text.Equals(""))
        {
            if (flag == false)
            {
                sql1 = sql1 + " and Ref LIKE N'%" + txtRef.Text + "%'";
                flag = true;

            }
            else
            {
                sql1 = sql1 + "  and Ref LIKE N'%" + txtRef.Text + "%'";
            }
        }

您无需在每个if条件中添加“where”,您需要使用“and”关键字添加条件。