Keeping sort order on a Gridview while another is sorted.

  • Thread starter Thread starter Cindy Lee
  • Start date Start date
C

Cindy Lee

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.
 
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();
 
Quoc Linh said:
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?
 
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) {...}
 
Cindy said:
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
 
Mark Rae said:
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??
 
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
 
Quoc Linh said:
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.
 
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
 
Quoc Linh said:
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'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.

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

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top