473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Gridview Binding/Paging

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
8 8503
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lee | last post by:
Hello all, Fairly new to .net. Using VB.net/2005 I am having a bit of trouble implementing a data driven gridview with paging. I am binding the grid to a DataTable at runtime (which is...
2
by: | last post by:
Hello, I have a GridView in my ASP.NET 2.0 application that performs the paging feature perfect when I have it bound to a data source. But now I have it bound to a dataset and the paging...
6
by: Carlos Albert | last post by:
Hi everybody, I'm working with a gridview (4 bound columns and 1 template column, using databind from codebehind). It works just fine, but I tried to add paging, and when I click any paging...
5
by: Amit | last post by:
Hello, I have a simple search screen, with two drop-downs and a text box. There's also a GridView control that is using a SqlDataSource control to show the matching results. The SqlDataSource uses...
1
by: ABHIJIT B | last post by:
Hi, I am using 2 web pages parent and popup.PopUp window is opened using window.showModalDialog().This is client requiremnt they don't wnat window.open() In PopUp page I am using GridView and...
1
by: John A Grandy | last post by:
In regard to a GridView that must support searching, filtering, sorting, and paging ... There is a tradeoff in performing the sorting and paging in the database versus to creating a CLR sort...
1
by: Danny Ni | last post by:
Hi, Could someone point me to source sample C# code to do gridview custom paging? No ObjectDataSource please, the reason I don't want to use ODS is the page is already created using the...
0
by: Question123 | last post by:
Hello I am trying to implement Gridview Custom Paging by using ObjectDataSource with properties How to pass parameters for SelectMethod and SelectCountMethod of ObjectDataSource .?? --...
9
by: ally | last post by:
Hi, I am programmatically binding gridview to a datatable. I want to hide 2 columns. I can be done by adding code in RowCreated event of gridview like e.Row.Cells.Visible = false; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.