-2

how I do a sorting in a gridView with data bound by a ObjectDataSource?

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

2 Answers2

1

Here is a question that has been previously answered.

For the actual sorting, you would call

collectionOfObjects.OrderBy(x => x.PropertyToSortOn);

You could use a switch to change what to sort on based on what is passed into the method via the args. So it would look a little more like this

switch(propertyName)
{
  case "property1":
    collectionOfObjects.OrderBy(x => x.PropertyToSortOn);
    break;
  case "property2":
    collectionOfObjects.OrderBy(x => x.OtherPropertyToSortOn);
    break;

  ...

}

Hope this helps! :)

Community
  • 1
  • 1
khr055
  • 28,690
  • 16
  • 36
  • 48
  • I don't understand the answer, I allready read this. – PassionateDeveloper Feb 02 '11 at 16:02
  • 1
    If the GridView and the ObjectDataSource are already bound, then the first steps you should take are to AllowSorting on the GridView. After that, you need to write the method for how to sort and hook up the event handler. – khr055 Feb 02 '11 at 16:05
  • allowSorting = true is done, no problem. With the rest I screwed up since 8 hours, can you help me more please – PassionateDeveloper Feb 02 '11 at 16:18
  • What you would need to do next is in your code behind, set your event to use the method that is actually going to do the sorting, like this: gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting); (from the article, btw) and make sure you have the method available so that the event handler will know what you're talking about. private void gridView_Sorting(object sender, GridViewSortEventArgs e) { //Sorting logic here } (also, from previous answer). So now when you try to sort from your grid view, this code will be called, and then it would be up to you just do the sorting – khr055 Feb 02 '11 at 16:21
  • Let me know if you need further help with this one. It can really twist your brain, the first couple of times setting this kind of thing up. :) – khr055 Feb 02 '11 at 16:24
  • I have done this all since 8-9 hours... the problem is the sorting method itself. Do you have skype / ICQ or something? – PassionateDeveloper Feb 02 '11 at 16:26
  • Ah I see... I thought the problem was with the event handlers. I'm editing my answer for further details on sorting... – khr055 Feb 02 '11 at 16:30
  • The problem is 1) I cant bind my datasource on the gridView after Soting, because I have a datasourceID from the objectDataSource. I could, of course, set thedataSourceID to null, but then I can't delete any more, can I? Because the deleting is done from the ODS. – PassionateDeveloper Feb 02 '11 at 16:38
0

If is more easy for you, why dont you try to sort it from the store procedure or the query. Maybe is not the optimun solution but it could be easier.

EDIT

IF you want to do it programattically with the gridview controls, Take a look at this code:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }


        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dtSortTable = GridView1.DataSource as DataTable;
            if (dtSortTable != null)
            {
                DataView dvSortedView = new DataView(dtSortTable);
                dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
                GridView1.DataSource = dvSortedView;
                GridView1.DataBind();
            }
        }

        private string getSortDirectionString(SortDirection sortDireciton)
        {
             string newSortDirection = String.Empty;
             if (sortDireciton == SortDirection.Ascending)
            {
                newSortDirection = "ASC";
            }
            else
            {
                newSortDirection = "DESC";
            }
            return newSortDirection;
        }
  • But How I can sort there and event by click ? – PassionateDeveloper Feb 02 '11 at 16:19
  • Maybe can use a dropdown ala AMAZON, and on the OnSelectedIndexChanged reload the page, sending the selected value of the dropdown as the field you want to sort the query by, It is not ajax elegant... but it might work and can ge easy to undersatand for your Users – Gerardo Jaramillo Feb 02 '11 at 23:31