CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2004
    Posts
    12

    Sorting By Multiple Columns How Can i Do i?

    I have some problem with sorting in multiple columns. I tried duplicating the the events etc but it still won't work. It tends to only sort by one expression. For example, i have 2 columns "name" and "company". When i click name, it will sort in ascending order of the names, which is correct but when i tried sorting by company, it sorts by name which isn't correct.

    The code below is for sorting only by name.
    Code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    	if (!Page.IsPostBack) 
    	{ 
    	                ViewState["sortField"] = "Name";
    	                ViewState["sortDirection"] = "DESC";
    		BindGrid();
    	}
    }
    
    private void BindGrid()
    {
                    SqlCommand cmd = sqlConnection1.CreateCommand();
    	cmd.CommandText = "SELECT * FROM Factory.Company ORDER BY " + ViewState["sortField"].ToString() + " " + ViewState["sortDirection"].ToString();
    	SqlDataAdapter sqlDataAdapter11 = new SqlDataAdapter();
    	sqlDataAdapter11.SelectCommand = cmd;
    	DataSet ds = new DataSet();
    	sqlConnection1.Open();
    	sqlDataAdapter11.Fill(ds, "Company");
    	sqlConnection1.Close();
    	DataGrid5.DataSource = ds;
    	DataGrid5.DataBind();
    }
    private void DataGrid5_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    	DataGrid5.CurrentPageIndex = e.NewPageIndex;
    	BindGrid();
    }
    
    private void DataGrid5_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
                    if(e.SortExpression.ToString() == ViewState["sortField"].ToString())
    	{
    		switch(ViewState["sortDirection"].ToString())       
    		{         
    			case "ASC":   
    			ViewState["sortDirection"] = "DESC";
    			break;
    
    			case "DESC":            
    			ViewState["sortDirection"] = "ASC";
    			break;
    		}
    	}
    	else
    	{
    		ViewState["sortField"] = e.SortExpression;
    		ViewState["sortDirection"] = "DESC";
    	}
    		BindGrid();
    	}
    What should be added in order to make multi column sorting work? or i have to re-write the whole program?

    Help needed thanks in advance

    -John
    Last edited by Eradicate; November 30th, 2004 at 04:47 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Sorting By Multiple Columns How Can i Do i?

    U need some kind of collection (not a single value):

    For example something like this:

    Code:
     
    ViewState["sortFields"][0].FieldName = "Name1";
    ViewState["sortFields"][1].FieldName = "Name2";
    ViewState["sortFields"][1].Ascending= false; 
    .....
    cmd.CommandText = "SELECT * FROM Factory.Company ORDER BY " + ViewState["sortFields"].ToString() ;
    it should build SQL like this:

    Code:
     
    SELECT * FROM Factory.Company ORDER BY Name1,Name2 DESC
    Best regards,
    Krzemo.

  3. #3
    Join Date
    Nov 2004
    Posts
    12

    Re: Sorting By Multiple Columns How Can i Do i?

    After making the collection, how do i specify the cloumn name will sort by name and the company column will sort by company. Without them sticking together as a single sort expression?

  4. #4
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Sorting By Multiple Columns How Can i Do i?

    I don't undestand what U wrote...

    how do i specify the cloumn name will sort by name and the company column will sort by company
    IN SQL:
    Code:
    SELECT * FROM Factory,Company ORDER BY name,company
    in c# U add Factory and Company to sort fields collection.

    When U build SQL expression U must iterate through sort fields collection and concatenate strings to compose ORDER BY clause.

    Without them sticking together as a single sort expression
    They have to stick together (in SQL expression).

    Hope it helps.

  5. #5
    Join Date
    Nov 2004
    Posts
    12

    Thumbs up Re: Sorting By Multiple Columns How Can i Do i?

    Sorry for some mistakes in my last post lol. What im trying to say is.
    Right now, i have 2 sort column "Name" & "Company". I've created 2 sort expressions named "SortName" and "SortCompany". But the problem is, How do i specify that "SortName" is for sorting the column "Name" and "SortCompany" is for sorting Column "Company" ?

    The code i attached on my first post, is my original codes which does only 1 column sorting.

    I don't think it is wise to post the 2 column sorting codes because it doesn't even runs just errors .

    Well sorry for my poor english. Hope you can understand better of my problem now. Thanks for replying.

  6. #6
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Sorting By Multiple Columns How Can i Do i?

    Sorry I'm not shure I understand it now.

    First thing:

    U have problem with SQL or C#?

    Is this SQL (below) working or not?
    Code:
    SELECT * FROM Factory.Company ORDER BY name,company
    Sorry for my poor english too .

  7. #7
    Join Date
    Nov 2004
    Posts
    12

    Re: Sorting By Multiple Columns How Can i Do i?

    The problem is, i have no idea. How to activate the 2 sort expression when the event handler on the datagrid only allow me to choose one sort expression. After adding
    Code:
    SELECT * FROM Factory.Company ORDER BY name,company
    Which settings and commands should i change or add in order for 2 sort expression to activate on both columns?

  8. #8
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: Sorting By Multiple Columns How Can i Do i?

    i have no idea. How to activate the 2 sort expression when the event handler on the datagrid only allow me to choose one sort expression.
    1 scenario:

    * at start collection is empty so it should be no order by clause
    * when user click at column company then,because there is no entries in collection, add entry "company" to collection. So your's code should generate:
    Code:
    ORDER BY company
    * when user click name it again failed to find entry, so entry should be added to collection (preferably on begining, if U add it on end U have to construct backward loop).
    Now generated code looks like:
    Code:
     ORDER BY name,company
    * user again clicked on name and it is already in collection ,so ensure that it is first element in collection and toggle it sorting direction
    Now generated code looks like:
    Code:
     ORDER BY name DESC,company
    * user clicked on company and it is already in collection ,so ensure that it is first element in collection and toggle it sorting direction
    Now generated code looks like:
    Code:
     ORDER BY company DESC,name DESC
    or (if U prefer) dont toggle if it is not first element in collection.

    * etc.

    Hope it helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured