By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,573 Members | 3,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,573 IT Pros & Developers. It's quick & easy.

Keeping sort order on a Gridview while another is sorted.

P: n/a
When I do a sorta on 1 table, then the other table goes back to the original
order.

What can I set so, it keeps the order of the other current gridview's order.

I set all the gridview values in my 'onpageload' in the cs file.
Feb 14 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Feb 14, 3:06 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
When I do a sorta on 1 table, then the other table goes back to the original
order.

What can I set so, it keeps the order of the other current gridview's order.

I set all the gridview values in my 'onpageload' in the cs file.
Have you tried create different views from the same data table:

DataView view1 = new DataView(YourDataTable);
view1.Sort = "ColumnNameToBeSorted ASC";
Grid1.DataSource = view1;
Grid1.DataBind();

DataView view2 = new DataView(YourDataTable);
view2.Sort = "SomeOtherColumnNameToBeSorted ASC";
Grid2.DataSource = view2;
Grid2.DataBind();

Feb 14 '07 #2

P: n/a

"Quoc Linh" <le********@yahoo.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
On Feb 14, 3:06 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
When I do a sorta on 1 table, then the other table goes back to the
original
order.

What can I set so, it keeps the order of the other current gridview's
order.

I set all the gridview values in my 'onpageload' in the cs file.

Have you tried create different views from the same data table:

DataView view1 = new DataView(YourDataTable);
view1.Sort = "ColumnNameToBeSorted ASC";
Grid1.DataSource = view1;
Grid1.DataBind();

DataView view2 = new DataView(YourDataTable);
view2.Sort = "SomeOtherColumnNameToBeSorted ASC";
Grid2.DataSource = view2;
Grid2.DataBind();
I'm not sure if that would help. I'm guessing it has something to do with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the current
gridview settings?
Feb 15 '07 #3

P: n/a
"Cindy Lee" <ci******@hotmail.comwrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something to do with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the current
gridview settings?
Two ways:

1) Store the sort order in ViewState

2) Use Ajax

Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}
Feb 15 '07 #4

P: n/a
Cindy Lee wrote:
"Quoc Linh" <le********@yahoo.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
>On Feb 14, 3:06 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
>>When I do a sorta on 1 table, then the other table goes back to the
original
>>order.

What can I set so, it keeps the order of the other current gridview's
order.
>>I set all the gridview values in my 'onpageload' in the cs file.
Have you tried create different views from the same data table:

DataView view1 = new DataView(YourDataTable);
view1.Sort = "ColumnNameToBeSorted ASC";
Grid1.DataSource = view1;
Grid1.DataBind();

DataView view2 = new DataView(YourDataTable);
view2.Sort = "SomeOtherColumnNameToBeSorted ASC";
Grid2.DataSource = view2;
Grid2.DataBind();


I'm not sure if that would help. I'm guessing it has something to do with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the current
gridview settings?
Each DataView maintains its own sort order, so in the page load link
each table to its own DataView, just as

Quoc Linh showed you.

T

Feb 15 '07 #5

P: n/a

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
"Cindy Lee" <ci******@hotmail.comwrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something to do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the current
gridview settings?

Two ways:

1) Store the sort order in ViewState

2) Use Ajax

Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}

I use the (!IsPostback), but everytime I use Gridviews, I get an empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?

DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??
Feb 15 '07 #6

P: n/a
On Feb 14, 6:21 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
"Mark Rae" <m...@markNOSPAMrae.comwrote in message

news:eb**************@TK2MSFTNGP03.phx.gbl...
"Cindy Lee" <cindy...@hotmail.comwrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something to do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the current
gridview settings?
Two ways:
1) Store the sort order in ViewState
2) Use Ajax
Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}

I use the (!IsPostback), but everytime I use Gridviews, I get an empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?

DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??
Getting an empty dataset in postback is by-designed because the server
won't persist anything unless you save your dataset in ViewState or
Session variable, otherwise, you'll risk re-querying the database to
rebuild your dataset in every postback. The ViewState however won't
persist object that is not serializabled, which the DataSet is, but
not the DataView. The problem with saving a dataset to a page's
ViewState is the content of the dataset is embedded in the page, which
will severely hinder performance in every postback (Imagine 1 million
rows converted to gibberish text that embedded in your page, un-
neccessarily increase the size of your page to be render in the
browser...)

I particularly don't like sorting a grid that requires postback.
Plainly put, it sucks. There are existing javascripts that would help
you do that.

Quoc Linh

Feb 15 '07 #7

P: n/a

Here's a couple links that I think might help you:

Read more about Javascript and ASP.Net:
http://www.builderau.com.au/program/...9130406,00.htm

ASP.Net grid are actually rendered as HTML table with built-in
javascript functions. Use this javascript library to sort your html
table.
http://kryogenix.org/code/browser/sorttable/

Quoc Linh

Feb 15 '07 #8

P: n/a

"Quoc Linh" <le********@yahoo.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
On Feb 14, 6:21 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
"Mark Rae" <m...@markNOSPAMrae.comwrote in message

news:eb**************@TK2MSFTNGP03.phx.gbl...
"Cindy Lee" <cindy...@hotmail.comwrote in message
>news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something to
do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the
current
gridview settings?
Two ways:
1) Store the sort order in ViewState
2) Use Ajax
Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}
I use the (!IsPostback), but everytime I use Gridviews, I get an empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?

DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??

Getting an empty dataset in postback is by-designed because the server
won't persist anything unless you save your dataset in ViewState or
Session variable, otherwise, you'll risk re-querying the database to
rebuild your dataset in every postback. The ViewState however won't
persist object that is not serializabled, which the DataSet is, but
not the DataView. The problem with saving a dataset to a page's
ViewState is the content of the dataset is embedded in the page, which
will severely hinder performance in every postback (Imagine 1 million
rows converted to gibberish text that embedded in your page, un-
neccessarily increase the size of your page to be render in the
browser...)

I particularly don't like sorting a grid that requires postback.
Plainly put, it sucks. There are existing javascripts that would help
you do that.

Quoc Linh
So to enable sorting on the gridview is useless if you have more than 1
table?
Then most people sort on the dataTable before it's binded and have sortable
= false on the gridview.
I guess I could have a session varible of the sort column for each table.
Eventually I'll probably use AJAX, but I don't have the time to set it up
right now.

Oh, also of note, I take my data from an XML stream, so it's not tied into
the database.



Feb 15 '07 #9

P: n/a
On Feb 15, 10:49 am, "Cindy Lee" <cindy...@hotmail.comwrote:
"Quoc Linh" <lequocl...@yahoo.comwrote in message

news:11**********************@l53g2000cwa.googlegr oups.com...
On Feb 14, 6:21 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
"Mark Rae" <m...@markNOSPAMrae.comwrote in message
>news:eb**************@TK2MSFTNGP03.phx.gbl...
"Cindy Lee" <cindy...@hotmail.comwrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something to
do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the
current
gridview settings?
Two ways:
1) Store the sort order in ViewState
2) Use Ajax
Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}
I use the (!IsPostback), but everytime I use Gridviews, I get an empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?
DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??
Getting an empty dataset in postback is by-designed because the server
won't persist anything unless you save your dataset in ViewState or
Session variable, otherwise, you'll risk re-querying the database to
rebuild your dataset in every postback. The ViewState however won't
persist object that is not serializabled, which the DataSet is, but
not the DataView. The problem with saving a dataset to a page's
ViewState is the content of the dataset is embedded in the page, which
will severely hinder performance in every postback (Imagine 1 million
rows converted to gibberish text that embedded in your page, un-
neccessarily increase the size of your page to be render in the
browser...)
I particularly don't like sorting a grid that requires postback.
Plainly put, it sucks. There are existing javascripts that would help
you do that.
Quoc Linh

So to enable sorting on the gridview is useless if you have more than 1
table?
Then most people sort on the dataTable before it's binded and have sortable
= false on the gridview.
I guess I could have a session varible of the sort column for each table.
Eventually I'll probably use AJAX, but I don't have the time to set it up
right now.

Oh, also of note, I take my data from an XML stream, so it's not tied into
the database.
Oh, also of note, I take my data from an XML stream, so it's not tied into
the database.
However method you used to populate your dataset, you'll have to
repopulate them if you don't persist your dataset in Session or
ViewState, it's your choice of implementation.

The rest is setting up break points in your code and see which objects
you cared about is still valid and which isn't. Especially check out
value of yourGrid.DataSource property in the next postback.

Page_Load(...)
{

if (!IsPostBack)
{
//Initial page load code gets execute here, only once (in most
cases)
}
else
{
//Code you want ONLY happen in post-back
}

//Code you want to happen, regardless of page loading or post back

}

Set your breakpoint in these blocks and see how they got triggered
when you perform an action on the client. I think it will be clear
then which object is available to you and which is not.

Another trick is after your page is rendered by the server, do a View
Source to view the source in the page, to see how your asp.net
controls are convert to its html equivalent by the server.

Good luck,

Quoc Linh

Feb 15 '07 #10

P: n/a

"Quoc Linh" <le********@yahoo.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
On Feb 15, 10:49 am, "Cindy Lee" <cindy...@hotmail.comwrote:
"Quoc Linh" <lequocl...@yahoo.comwrote in message

news:11**********************@l53g2000cwa.googlegr oups.com...
On Feb 14, 6:21 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
"Mark Rae" <m...@markNOSPAMrae.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
"Cindy Lee" <cindy...@hotmail.comwrote in message
>news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something
to
do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the
current
gridview settings?
Two ways:
1) Store the sort order in ViewState
2) Use Ajax
Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}
I use the (!IsPostback), but everytime I use Gridviews, I get an
empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?
DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??
Getting an empty dataset in postback is by-designed because the server
won't persist anything unless you save your dataset in ViewState or
Session variable, otherwise, you'll risk re-querying the database to
rebuild your dataset in every postback. The ViewState however won't
persist object that is not serializabled, which the DataSet is, but
not the DataView. The problem with saving a dataset to a page's
ViewState is the content of the dataset is embedded in the page, which
will severely hinder performance in every postback (Imagine 1 million
rows converted to gibberish text that embedded in your page, un-
neccessarily increase the size of your page to be render in the
browser...)
I particularly don't like sorting a grid that requires postback.
Plainly put, it sucks. There are existing javascripts that would help
you do that.
Quoc Linh
So to enable sorting on the gridview is useless if you have more than 1
table?
Then most people sort on the dataTable before it's binded and have
sortable
= false on the gridview.
I guess I could have a session varible of the sort column for each
table.
Eventually I'll probably use AJAX, but I don't have the time to set it
up
right now.

Oh, also of note, I take my data from an XML stream, so it's not tied
into
the database.
Oh, also of note, I take my data from an XML stream, so it's not tied
into
the database.

However method you used to populate your dataset, you'll have to
repopulate them if you don't persist your dataset in Session or
ViewState, it's your choice of implementation.

The rest is setting up break points in your code and see which objects
you cared about is still valid and which isn't. Especially check out
value of yourGrid.DataSource property in the next postback.

Page_Load(...)
{

if (!IsPostBack)
{
//Initial page load code gets execute here, only once (in most
cases)
}
else
{
//Code you want ONLY happen in post-back
}

//Code you want to happen, regardless of page loading or post back

}

Set your breakpoint in these blocks and see how they got triggered
when you perform an action on the client. I think it will be clear
then which object is available to you and which is not.

Another trick is after your page is rendered by the server, do a View
Source to view the source in the page, to see how your asp.net
controls are convert to its html equivalent by the server.

Good luck,

Quoc Linh
Thanks, the postback call just does 1 gridview
__doPostBack('GridView2', 'Sort$col1)

is there anyway to remember the last postback variables. I have no problem
with storing the data in a session variable or a view state, because my olap
queries take a long time.
Feb 15 '07 #11

P: n/a
On Feb 15, 12:31 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
"Quoc Linh" <lequocl...@yahoo.comwrote in message

news:11**********************@m58g2000cwm.googlegr oups.com...
On Feb 15, 10:49 am, "Cindy Lee" <cindy...@hotmail.comwrote:
"Quoc Linh" <lequocl...@yahoo.comwrote in message
>news:11**********************@l53g2000cwa.googleg roups.com...
On Feb 14, 6:21 pm, "Cindy Lee" <cindy...@hotmail.comwrote:
"Mark Rae" <m...@markNOSPAMrae.comwrote in message
>news:eb**************@TK2MSFTNGP03.phx.gbl...
"Cindy Lee" <cindy...@hotmail.comwrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
I'm not sure if that would help. I'm guessing it has something
to
do
with
calling it on page load and how the post backs work.
Sorting always does a postback, so is there a way to keep the
current
gridview settings?
Two ways:
1) Store the sort order in ViewState
2) Use Ajax
Also, make sure that any code you don't want to run on postback is
surrounded by if (!IsPostback) {...}
I use the (!IsPostback), but everytime I use Gridviews, I get an
empty
dataset when I sort.
Is there a command that sets the current viewstate, to the last one?
DataView pageview = ViewState[currentView]???
GridView2.DataSource = pageview??
Getting an empty dataset in postback is by-designed because the server
won't persist anything unless you save your dataset in ViewState or
Session variable, otherwise, you'll risk re-querying the database to
rebuild your dataset in every postback. The ViewState however won't
persist object that is not serializabled, which the DataSet is, but
not the DataView. The problem with saving a dataset to a page's
ViewState is the content of the dataset is embedded in the page, which
will severely hinder performance in every postback (Imagine 1 million
rows converted to gibberish text that embedded in your page, un-
neccessarily increase the size of your page to be render in the
browser...)
I particularly don't like sorting a grid that requires postback.
Plainly put, it sucks. There are existing javascripts that would help
you do that.
Quoc Linh
So to enable sorting on the gridview is useless if you have more than 1
table?
Then most people sort on the dataTable before it's binded and have
sortable
= false on the gridview.
I guess I could have a session varible of the sort column for each
table.
Eventually I'll probably use AJAX, but I don't have the time to set it
up
right now.
Oh, also of note, I take my data from an XML stream, so it's not tied
into
the database.
Oh, also of note, I take my data from an XML stream, so it's not tied
into
the database.
However method you used to populate your dataset, you'll have to
repopulate them if you don't persist your dataset in Session or
ViewState, it's your choice of implementation.
The rest is setting up break points in your code and see which objects
you cared about is still valid and which isn't. Especially check out
value of yourGrid.DataSource property in the next postback.
Page_Load(...)
{
if (!IsPostBack)
{
//Initial page load code gets execute here, only once (in most
cases)
}
else
{
//Code you want ONLY happen in post-back
}
//Code you want to happen, regardless of page loading or post back
}
Set your breakpoint in these blocks and see how they got triggered
when you perform an action on the client. I think it will be clear
then which object is available to you and which is not.
Another trick is after your page is rendered by the server, do a View
Source to view the source in the page, to see how your asp.net
controls are convert to its html equivalent by the server.
Good luck,
Quoc Linh

Thanks, the postback call just does 1 gridview
__doPostBack('GridView2', 'Sort$col1)

is there anyway to remember the last postback variables. I have no problem
with storing the data in a session variable or a view state, because my olap
queries take a long time.
I think I might know why you're puzzled. The checking for IsPostBack
isn't straight forward. Try the following logic:

///////////////////////////////////////////////////
///// In your Page_Load event ///////

protected override void Page_Load(object sender, EventArgs e)
{

DataSet MyDataSet;

if (!IsPostBack)
{
MyDataSet = PerformSomeMethodToPopulateMyDataSet(); //This populate
the dataset once and gets executed once
//Persist in in Session
Session["MyDataSet"] = MyDataSet;
}

//Here, expects Session["MyDataSet"] is valid, always, when page_load
event is triggerd
if (Session["MyDataSet"] == null)
{
throw new ArgumentNullException("Expects Session[\"MyDataSet\"] to
have a value.");
}

//Retrieved the saved dataset
MyDataSet = (DataSet) Session["MyDataSet"];

//Bind to grid, executes every time.
// It's your choice to bind to view for sorting or filtering purpose
myGrid.DataSource = MyDataSet;
myGrid.DataBind;

} //ends page_load

HTH,
Quoc Linh

Feb 15 '07 #12

P: n/a
Thanks for all the help. It was my bad, I should have looked more at the
sorting functions that I use (I use custom ones). So I changed those a
little on my sorting event: (Also I like putting my data in a session
variable cus my page loads better)

protected void gridView1_Sorting(object sender, GridViewSortEventArgs e)
{
GridViewSortExpression1 = e.SortExpression;
int pageIndex = GridView1.PageIndex;
GridView1.DataSource = SortDataTable1(GridView1.DataSource as
DataTable, false);
Session["DS1"] = GridView1.DataSource;
GridView1.DataBind();
GridView1.PageIndex = pageIndex;
if (Session["DS2"] != null)
{
GridView2.DataSource = Session["DS2"];
GridView2.DataBind();
}

}
Feb 15 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.