1

I am trying to sort GridView based on click in header, when paging is alowed.

How to retrieve content of the grid?

GridView.Rows only gives me rows on the selected page.

I don't want to do another select from Database and than order it. And also, don't want to save it in SessionState.

I can't figure out what 'default' SortExpression looks like.

And this SortExpression will order DESC or ASC by ID, but how to do some custom sort ? Or to sort by other type?

How to sort by ValueFromIncluded

//example
     <asp:GridView runat="server"
                    ID="GridID" 
                    SelectMethod="GridView_GetData"
                    ItemType="ItemType"           
                     AllowPaging="true">           
                    <Columns>
                        <asp:TemplateField HeaderText="Id" SortExpression="Id">
                            <ItemTemplate>
                               <asp:Label runat="server" Text='<%# Item?.Id%>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
asp:TemplateField HeaderText="ValueFromIncluded" SortExpression="ValueFromIncluded">
                            <ItemTemplate>
                               <asp:Label runat="server" Text='<%# Item?.ValueFromIncluded.SomeOtherId%>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
            </Columns>
            </asp:GridView>
            // SelectMethod.
          public IQueryable<ItemType> GridView_GetData()
                {           
                    IQueryable<ItemType> q = dbx.ItemType               
                        .Include(f => f.OtherType);
            }
6
  • I would suggest to let the Database layer do the sorting and paging. Commented Apr 14, 2016 at 21:49
  • But I oldery have that results from GridView_GetData() I just want to pass my custom sort. But I am getting error Instance property 'CustomStringInSortExpression' is not defined for type 'ItemType' Ofcourse DB should handle that, I just want to say how to oreder. Commented Apr 14, 2016 at 21:54
  • You need to put a switch case on the SortExpression and then call appropriate OrderBy on the specific property of ItemType Commented Apr 14, 2016 at 21:56
  • And when I do that, I gett error that I have typed above Commented Apr 14, 2016 at 22:07
  • Disable paging, rebind the gridview, sort programmatically, then enable paging Commented Apr 14, 2016 at 22:08

1 Answer 1

1

So, what you need to do is to make another class and use it in Grid. Create new class ItemTypeForGrid , and on aspx page ItemType="ItemType+ItemTypeForGrid"

 public class ItemTypeForGrid: ItemType
        {
            public string ValueFromIncluded{ get; internal set; }

        }

So the code will look like

 <asp:GridView runat="server"
       ID="GridID" 
       SelectMethod="GridView_GetData"
       ItemType="ItemType+ItemTypeForGrid"           
       AllowPaging="true">           
    <Columns>
          <asp:TemplateField HeaderText="Id" SortExpression="Id">
                                    <ItemTemplate>
                                       <asp:Label runat="server" Text='<%# Item?.Id%>'/>
                                    </ItemTemplate>
                                </asp:TemplateField>
        asp:TemplateField HeaderText="ValueFromIncluded" SortExpression="ValueFromIncluded">
                                    <ItemTemplate>
                                       <asp:Label runat="server" Text='<%# Item?.ValueFromIncluded.SomeOtherId%>'/>
                                    </ItemTemplate>
                                </asp:TemplateField>
                    </Columns>
                    </asp:GridView>

// SelectMethod.

public IQueryable<ItemTypeForGrid> GridView_GetData()
  {           
    IQueryable<ItemType> q = context.ItemType               
                     .Include(f => f.OtherType);
    // AND HERE SELECT WHAT YOU NEED IN GRID
    var qForGrid = q.Select( s=> new ItemTypeForGrid(){
                   ValueFromIncluded = r.ItemType.OtherType });
    }

This way you can sort! But more important thing is that it selects only what needs to be seen in GridView, not all properties from all included tables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.