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

Gridview Binding/Paging

P: n/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
--

AG
Email: discuss at adhdata dot com


Oct 2 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 2 '06 #2

P: n/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

"Luke Zhang [MSFT]" <lu******@online.microsoft.comwrote in message
news:vp**************@TK2MSFTNGXA01.phx.gbl...
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 2 '06 #3

P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 4 '06 #4

P: n/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?

--

AG
Email: discuss at adhdata dot com

"Luke Zhang [MSFT]" <lu******@online.microsoft.comwrote in message
news:6i**************@TK2MSFTNGXA01.phx.gbl...
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 4 '06 #5

P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 5 '06 #6

P: n/a
AG
Ok, lets say I disable paging or want to handle paging myself.
How do I disable automatic binding?

--

AG
Email: discuss at adhdata dot com

"Luke Zhang [MSFT]" <lu******@online.microsoft.comwrote in message
news:wf**************@TK2MSFTNGXA01.phx.gbl...
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 5 '06 #7

P: n/a
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.Em pty );

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/lib...rols.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.

Oct 6 '06 #8

P: n/a
AG
Thank you.
I will try that.

--

AG
Email: discuss at adhdata dot com

"Luke Zhang [MSFT]" <lu******@online.microsoft.comwrote in message
news:ND**************@TK2MSFTNGXA01.phx.gbl...
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.Em pty );

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/lib...rols.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.

Oct 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.