Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2005, 09:58 AM
Authorized User
 
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting a DataGrid.DataSource to a DataView

All:
I am trying to convert a DataGrid's DataSource to a DataView during the preRender event of the same DataGrid. My "catch" gets an InvalidCastException (Specified Cast is not valid). The ultimate point of the conversion is to get a count of the number of records in the DataGrid. My single line of code:

Me.labTest.Text =
CType(DG1.DataSource, DataView).count.ToString() + " records found"

Can anyone help me with this?

TIA,

Demivolt
 
Old July 7th, 2005, 01:12 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What is the datagrid's datasource?

 
Old July 7th, 2005, 01:25 PM
Authorized User
 
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

JBenson001:

The DataSource of my DataGrid is a DataSet drawn from a single
SQL Server 2000 Database. The SQL connection, query string and
adapter are located in function called from a VB module within my project.

Thanks,

Demivolt


 
Old July 7th, 2005, 01:29 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Me.labTest.Text = dataset.Tables(<table name or index>).Rows.Count

 
Old July 7th, 2005, 02:58 PM
Authorized User
 
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Getting closer. I tried your suggested syntax --

 Me.labTest.Text = dataset.Tables(<table name or index>).Rows.Count

This returns ALL of the records in the table [tables(0)]. I am
wanting a count of only those records going to my DataGrid, i.e. the DataSource of my grid. The only records show up on my grid are based on a criterion triggered by selection from a dropdownList, which sets up a "where" clause in my SQL Select string.

I am thinking that I may be confused about what data is available during a preRender event. Can controls not be created and inserted on a form "on-the-fly" during this even????

 
Old July 7th, 2005, 03:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Does this help: http://Imar.Spaanjaars.Com/QuickDocId.aspx?QUICKDOC=356


Imar
 
Old July 7th, 2005, 04:21 PM
Authorized User
 
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar:
I went to your URL. Your last paragraph was relevant to my case in that, unfortunately, I am using paging, and as stated, the count only returns a number equal to the PageSize.

Your last SENTENCE, however, sums up exactly what I need: " ... return the number of items in the original datasource that the control is bound to." Do you, or anyone, have a suggestion along these lines. Also, can the problem be my choice of the preRender Event?

Thank you,

Demivolt


 
Old July 7th, 2005, 04:46 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Either I am not understanding what you want, or you are missing a few concepts.

If you're not using paging, the DataGrid displays all records. Thus, the Items.Count returns all records currently displayed in the grid.

If you want to know all the records in the initial data source, ask the datasource ;)
Suppose you create a dataset with a query like this:

SELECT Col1, Col2 FROM YourTable WHERE SomeCriteria = SomeValue.

Let's say your *database* table holds 100 records, and that 20 are returned by this query. Let's also say your Grid has a PageSize of 10

Then this:

YourDataSet.Tables(0).Rows.Count

returns 20, as that is the number of records in a *DataTable* inside the DataSet, not the total number of records in the table in the *database*.

This:

MyDataGrid.Items.Count returns 10

and this:

SELECT COUNT(*) FROM YourTable


returns 100

So, now you can count whatever you want: items in the grid (Items.Count), items in the datasource (Tables(0).Rows.Count) or items in the database table (SELECT COUNT(*) FROM YourTable)

Is there anything more to count??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: You Think I Ain't Worth A Dollar, But I Feel Like A Millionaire by Queens of the Stone Age (Track 2 from the album: Songs For The Deaf) What's This?
 
Old July 8th, 2005, 09:33 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by demivolt
 I am thinking that I may be confused about what data is available during a preRender event.

I think the answer to the original question has been provided above, but I wanted to chime in with something.

The important thing to remember with a DataGrid is that the DataSource is only available during a page execution when the DataSource was set. It sounds like in your situation this is when you changed the selection of the DDL that establishes the criteria for the query. During a page execution where the DataSource has been set, you can interrogate this property, otherwise (postback not related to changing grid data) it will be null. Then you can't find out how many rows are in the grid's source data when paging is enabled. This is usually not a problem because you'd put the page count result into a label that is stateful and needn't be changed until you rebind the grid.

-Peter
 
Old July 8th, 2005, 09:57 AM
Authorized User
 
Join Date: Aug 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

GOT IT!

Imar, your reply pushed me in a direction away from the preRender event and toward a closer inspection of how I was handling my DataSet.
I had a superfluous call to the database in the postback that was giving me unintended results--this was along the lines of what Planoie pointed out later.

Gentlemen, I truly appreciate your help. Have a pleasant weekend. I know I will, now.

Demivolt






Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid or Dataview cp75 Beginning PHP 2 September 26th, 2008 10:56 AM
Duplicating datagrid rows using dataview batlou Visual Studio 2005 0 October 25th, 2006 11:41 AM
.NET 2 DataGrid w/DataView batlou VS.NET 2002/2003 0 October 25th, 2006 11:40 AM
DataTable from DataGrid.DataSource Jamal Junior ASP.NET 2.0 Basics 1 March 24th, 2006 01:51 AM
Datagrid functionality without a datasource cesherman VS.NET 2002/2003 1 July 7th, 2003 11:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.