473,842 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Gridview Binding/Paging

AG
ASP.NET 2.0, VS 2005

I have a gridview with paging enabled, bound to an objectdatasourc e.
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 8541
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.PageI ndex = Math.Floor(28 / GridView1.PageS ize)

GridView1.Selec tedIndex = 28 Mod GridView1.PageS ize

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******@onlin e.microsoft.com wrote in message
news:vp******** ******@TK2MSFTN GXA01.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.PageI ndex = Math.Floor(28 / GridView1.PageS ize)

GridView1.Selec tedIndex = 28 Mod GridView1.PageS ize

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******@onlin e.microsoft.com wrote in message
news:6i******** ******@TK2MSFTN GXA01.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******@onlin e.microsoft.com wrote in message
news:wf******** ******@TK2MSFTN GXA01.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"
AutogenerateCol umns to "true'

and then add following code;

protected void Page_Load(objec t sender, EventArgs e)
{
if (!this.IsPostBa ck)
{
SqlDataSource1. Select(DataSour ceSelectArgumen ts.Empty );

GridView1.DataS ource = SqlDataSource1;
GridView1.DataB ind();

}
}

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.enablecach ing.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******@onlin e.microsoft.com wrote in message
news:ND******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hello,

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

DataSourceID to blank
AllowPaging to "False"
AutogenerateCol umns to "true'

and then add following code;

protected void Page_Load(objec t sender, EventArgs e)
{
if (!this.IsPostBa ck)
{
SqlDataSource1. Select(DataSour ceSelectArgumen ts.Empty );

GridView1.DataS ource = SqlDataSource1;
GridView1.DataB ind();

}
}

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.enablecach ing.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
1692
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 created on the fly) as opposed to setting up a datasource on the page, etc. I create the datatable and then assign it to the gridview's datasource property and call DataBind.
2
16347
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 feature will not work. When I try to use paging I get this error: The GridView 'gvResults' fired event PageIndexChanging which wasn't handled.
6
6515
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 button (next, last, or page #), it return this error and I don't know what's wrong: Source Error: An unhandled exception was generated during the execution of the
5
11235
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 the control values in its query as parameters. Two questions: 1. When the Find button is clicked, how do I tell the GridView to load the data? 2. How do I stop the GridView from data binding when the page first loads? I don't want to...
1
1704
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 DataGrid and want to do Paging. Also I am using following code in parent form for Paging.Paging is possible if GridView or Datagrid AutoPost propery is set to true else
1
1963
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 expression and allowing the GridView to perform the sorting and paging. How do people feel about each option for (1) simple sorts (alphabetical, compound alphabetical)
1
4605
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 DataSource to do data binding, it will takes some efforts to convert it to use ODS. TIA
0
890
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 .?? -- Regards
9
12107
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; e.Row.Cells.Visible = false; but after that paging is not working. I tried another way by using gridview's column.Visible property but its giving error
0
9872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10314
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7038
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.