1392023093user
1392023093user

Reputation: 1097

Sorting GridView asp.net

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

Upvotes: 1

Views: 201

Answers (1)

helloWorld
helloWorld

Reputation: 315

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.

Upvotes: 1

Related Questions