【问题标题】:how to sort searched records in a gridview如何在gridview中对搜索到的记录进行排序
【发布时间】:2013-09-23 06:01:23
【问题描述】:

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

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 中对搜索到的记录进行排序,但是当它绑定记录时,它绑定了数据库表中的所有记录......而我只需要绑定和显示搜索到的记录。我不明白我哪里出错了。任何帮助都感激不尽。提前致谢。

【问题讨论】:

  • 你的 SearchTable() 函数在哪里?也可以加这个功能码吗?
  • @SainPradeep 感谢您的及时回复。我添加了 SearchTable() 供您查看。谢谢。
  • 请调试它并根据您的条件检查最终查询。
  • @SainPradeep 当我在 txtRef.Text 中输入 'h' 时,搜索查询是:"SELECT * from dbo.Documents1 where Ref LIKE N'%h%'"
  • 请查看我的回答,希望对您有所帮助。

标签: c# asp.net gridview


【解决方案1】:

请像这样更改您的 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”关键字添加条件。

【讨论】:

  • Sain Pradeep,我更改了代码。但是输出的行为仍然没有区别。我仍然无法对“搜索记录”进行排序。
  • 您的 e.SortExpression 包含什么?
  • 当我点击对“Date_Received”列进行排序时,e.SortExpression 包含值“Date_Received”
  • 它是数据库中的日期字段。那么您需要将其转换为 varchar 进行排序。请查看stackoverflow.com/questions/1545888/sql-order-by-date-problem
  • 我想知道为什么使用所有记录排序功能,“Date_Received”排序工作正常。我试图对sql中的varchar列“Ref”进行排序,但它既没有排序。
猜你喜欢
  • 2016-03-02
  • 2013-07-10
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
  • 2023-02-24
  • 2016-07-09
  • 1970-01-01
  • 2021-10-26
相关资源
最近更新 更多