Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having problem in sorting the searched records in a gridview to ascending and descending directions. I have applied the following code to normal gridview (all records) and it is working all fine but when I try to search any records from all records, and try to sort those searched records, following code neither performs any actions nor throws any errors: Following code is applied for sorting:

C#
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 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();

 }


Search function is :

C#
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 + "%'";
        }
    }


    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;

        }
    }
}


I cant understand where I am going wrong. when clicked on a column to sort, it took me out of searched records and sorted "All-Records" which are placed in GridView1 while searched-records are placed in GridView2 as shown in the code. what I am failed to understand is, why programs jumps to GridView1 and sort all-records there when I click to click to sort searched-records in GridView2. Any help will be much appreciated. Thanks in advance.
Posted
Updated 23-Sep-13 23:17pm
v3
Comments
Thanks7872 24-Sep-13 2:44am    
This is just a code dump nothing else.Remove unnecessary code block and explain the problem clearly.
Thomas ktg 24-Sep-13 3:32am    
Is sorting event fired in Server side?
[no name] 24-Sep-13 5:18am    
@Thomas Ktg , Thanks for replying.

Yes I have set GridView2 to allow sorting by selecting Sorting value true in GridView2 properties (if thats what You mean by your question)
Thomas ktg 24-Sep-13 5:29am    
Yes that's one thing you must do to allow sorting in gridview. Have tried setting a break point on Gridview_sorting event in the server side and check whether the raised event is fired or not?
[no name] 24-Sep-13 5:32am    
Yes I did set a breakpoint in GridView2_Sorting(), and inside it "sortExpression" = "name of the column I click on to sort it"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900