Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
My code looks like below. How to sort the gridview by clicking any of the column header? Let say sort it after done the search from gridview.
I already set the allow sorting = true and how should i code in below>


C#
SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select top 1000 machineNameDisplay, equipment_type, frequency, formStatus, NextDueDate, cal_eqid from dbo.Obs_PMChkList_1 where v_dept = 'BGA EOL'";

        //DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        DataView dv = new DataView();

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
        GridView1.DataSource = dt;
        dv = dt.DefaultView;
        //Session["values"] = dv;
        //BindData("");
        GridView1.DataBind();
        conn.Close();


C#
protected void btnSearch_Click(object sender, EventArgs e)
    {
        string searchString = txtSearch.Text;

        foreach (GridViewRow row in GridView1.Rows)
        {
            TableCellCollection cells = row.Cells;

            foreach (TableCell cell in cells)
            {
                //if (cell.Text.ToLower().StartsWith(searchString.ToLower()))
                if (cell.Text.ToLower().StartsWith(searchString.ToLower()))
                {
                    break;

                }
                else
                {
                    //does not meet
                    cell.Visible = false;
                }
            }
        }
    }



C#
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//How to code here?
}
Posted

nothing at all, It should working
 
Share this answer
 
Hi,

1. My search function seems does not working as expected. My expected result is display the whole row but it does not. For eg, i search for "abc" and the 2nd column of the row contains "abc", system only display from second column onwards. I need it to display in whole row (include 1st cell).

2. After this, i need to sort it if click on the header. It have to able to sort it from my search result.

Please help!
 
Share this answer
 
Comments
BabyOreo 5-Dec-12 2:08am    
Following code solve my 2nd issue:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = ((DataSet)Session["myDataSet"]).Tables[0];
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GridView1.DataSource = dt;
GridView1.DataBind();
}

private string GetSortDirection(string column)
{
string sortDirection = "DESC";
string sortExpression = ViewState["SortExpression"] as string;

if (sortExpression != null)
{
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "DESC"))
{
sortDirection = "ASC";
}
}
}

ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;

return sortDirection;
}
I solved issue 1 with flag:
Appreciate who can help my second issue with provide some sample.
Thanks!

C#
protected void btnSearch_Click(object sender, EventArgs e)
    {
        int flag;
        string searchString = txtSearch.Text;

        foreach (GridViewRow row in GridView1.Rows)
        {
            flag = 0;
            TableCellCollection cells = row.Cells;

            foreach (TableCell cell in cells)
            {
                //if (cell.Text.ToLower().StartsWith(searchString.ToLower()))
                if (cell.Text.ToLower().StartsWith(searchString.ToLower()))
                {

                    flag = 1;                    
                    break;

                }
                            }
             if (flag == 0 )
             {
                 row.Visible = false;

             }
        }
 
Share this answer
 

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