Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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>
 

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();
 
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;
                }
            }
        }
    }
 

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//How to code here?
}
Posted 25-Nov-12 22:16pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

nothing at all, It should working
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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!
  Permalink  
Comments
BabyOreo at 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;
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I solved issue 1 with flag:
Appreciate who can help my second issue with provide some sample.
Thanks!
 
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;
 
             }
        }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 499
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100