Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting on gridview 2

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
US
Hello,
I am trying to implement sorting on a gridview by using the following code -

Code:
protected void grdPerson_OnSorting(object sender, GridViewSortEventArgs e)
        {
            DataTable tb = myGird.DataSource as DataTable;
            
            
            if (tb != null)
            {
                DataView view = new DataView(tb);
                view.Sort = e.SortExpression + "" + getSortDirection(e.SortExpression) ;

                myGird.DataSource = view;
                myGird.DataBind();
            }
        }

The problem that I am having is the DataTable tb is returning null, so the sort isn't processing. Does anyone have any thoughts on why it is returning null?

Thanks for any help.

carl
MCSD, MCTS:MOSS
 
You will have to hit the database again to get your datatable. Or you can store the dt in session and reuse it, or if the dt does not change very often, you can use cache.
 
you need to fetch the data and sort it(either code or sql) and bind to the grid.

remember the web doesn't have state (despite the attempts of postback and viewstate). when you make a request you need to get the data from the source each time.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks - I am now able to sort if I retrieve the datasource in the above function. The only problem I am having now is that when I move to the next page, the data is no longer sorted.
I realize that is because I am retrieving a new set of data each time in my PageIndexChanging Event, but what should I use then to maintain the sorting? I guess I don't understand at what point I should be pulling my data and what dataset I should be using as the datasource for the grid.

Thanks

carl
MCSD, MCTS:MOSS
 
It sounds like you don't understand stateless nature of web development. take the time to understand how the web works (request/response, statelessness) and then research the asp.net pipeline. since you're using webforms you should also understand the Page lifecycle. this will give you the foundation to design websites using webforms. this will also be the founation for MVC framework and html view engines if you diverge from webforms in the future.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Jason - thanks for your help.
I have been able to get this working by storing the table and sort info in session variables. This should be fine for me as I will always be working on a single server as opposed to a farm, and there will never be more than 2 or 3 people using the application.
Correct me if I'm wrong with this next statement, but that is not exactly stateless because I am storing data in the session state. In order to make this trule stateless, I should query the datasource every time there is a postback.



carl
MCSD, MCTS:MOSS
 
You are not tyring to make anything stateless. What Jason is explaing is that web applications are stateless. In order to get around that, you need to do what you did with Session, or Cache, etc.
 
storing the sort operators in session is a good choice, however I would not store the table in session. when will the table be removed from session? how will the session value expire when the table is updated? if user 1 has value in session, but user 2 updates a record, user 1's session has no way to know this.

keep the sort operators in session, but query for the results each time.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Great - that's what I will do.

Thanks for the help guys!

carl
MCSD, MCTS:MOSS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top