Gridview Binding/Paging

A

AG

ASP.NET 2.0, VS 2005

I have a gridview with paging enabled, bound to an objectdatasource.
Both were dropped on to the page and configured via the wizards.
Basically working as expected.

The gridview's databind method is apparently called when the page is loaded
as I have no code calling the databind method.
How can I keep the gridview from databinding automatically and control it
myself?
For instance, it does not need to databind on a postback.

Regarding paging. How can I determine what page a particular record is on in
order to display that page and select the desired row?

TIA
 
L

Luke Zhang [MSFT]

Hello,

Since the paging has been enabled for the GridView control, we had better
leave all default binding settings. (When we click the page index to change
the current page of Gridview, this is also a postback event, so we also
need to bind the gridview and generate different pages in the event. )

To locate the exact record in the gridview, we need calculate the page and
row by ourselves, and set the gridview's pageindex and selectedindex . For
example, to locate the 28th records:

GridView1.PageIndex = Math.Floor(28 / GridView1.PageSize)

GridView1.SelectedIndex = 28 Mod GridView1.PageSize

If there is any further questions, please feel free to let me know.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Thanks for the reply Luke.

Are you saying that there is no way for me to control the bindings?
It seems a waste of resources to be binding more than necessary.
For instance, when using edit mode, the gridview is initially bound (making
a database call) sometime before the page_load event, and then bound again
(another database call), sometime after the RowEditing event.
I have to think this process can be made more efficient.

I understand what you are saying about calculating the page and my question
here is also related to the above situation.
In order to calculate the page and row I need to know the record number. It
seems to get that, I need to separately query the datasource, making another
database call.
Is there not some way to find the desired record number through some
property/method of the gridview and then use it to calculate the page? After
all, the gridview is somehow determining which records to show on the
current page.

For instance, in the case of adding a new record.
When the page posts back, the gridview is databound.
Then my code runs to add the record.
I must then rebind the gridview in order for the new record to be included.
If I the new record to be the selected record, I must separately query the
database and determine the row number.
Then I can calculate the page.
That is three database calls! There should be a way to do it in one.

--

AG
Email: discuss at adhdata dot com
 
L

Luke Zhang [MSFT]

Hello,

If the paging was not enabled for the gridview, we can enable the
"ViewState" of the Viewgrid, and only bind it when it is not post back. The
data will be keep in viewstate so that we don't need to requery it.
However, if the paging is enabled, paging information will be calculated on
every request, and viewstate only keep current page's data. Anyway, it
didn't query all data from the database, just the records on current page,
so I think it is still efficient here.

If you need to locate a record frequently, you may consider put the
dataset/datatable in Session so that you don't need to query the database
for every request. After adding a new record, the data in session need to
be updated as a update in database. This solution can work when the data in
the gidview/ dataset is not too much.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Hi Luke,

I guess I am a bit confused.
Paging is enabled and so is viewstate. I had thought viewstate needed to be
enabled for paging to work.
I am not trying to avoid all binding on postback, just the unnecessary
binding.
It does not make sense to bind the gridview, just to bind it again a second
later.

Are you saying that there is NO way for me to stop the griview from binding
automatically?
 
L

Luke Zhang [MSFT]

Hello,

When the paging is enabled, we have to leave griview bound automatically.
Basde on my experience, the most cost may be not the binding process, but
the query to database, you may consider the suggestion about adding data to
session for better performance and locating.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Ok, lets say I disable paging or want to handle paging myself.
How do I disable automatic binding?
 
L

Luke Zhang [MSFT]

Hello,

You may first set the GridView's DataSource to "none", and then change
following properties;

DataSourceID to blank
AllowPaging to "False"
AutogenerateColumns to "true'

and then add following code;

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{


SqlDataSource1.Select(DataSourceSelectArguments.Empty );

GridView1.DataSource = SqlDataSource1;


GridView1.DataBind();

}
}

Or, you may consider another approach: enable caching for the datasource,
you may leave all of your original settings, just select the SQLDataSource,
And set EnableCaching to "True":

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldataso
urce.enablecaching.aspx

This works when a user access same records repetitiously. It will first
check data in cache other than requery the database. But when you change
the records, the cache will be updated and requery will be performed.

Sincerely,

Luke Zhang

Microsoft Online Community Support
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top