Click to See Complete Forum and Search --> : Datagrid -- HasRows Help


macker
07-24-2007, 10:28 AM
I have a datagrid in ASP.NET, using VB 2005 and SQL Server 2005.

I am binding the datagrid to a dataset provided through SQL, and then binding the returned dataset to the datagrid.

Everything is working fine, however, when the dataset returned does not contain any rows, (the search returns nothing) the datagrid shows with only the header present.

I thought I remember a property, like

datagrid.HasRows

??? or something like that.

If the datagrid is going to be empty, I want to catch that, and just display a message.

Does anyone know this property for datagrids????

Thank you!!

lmf232s
07-24-2007, 01:01 PM
'Code to return dataset here
If ds.tables(0).Rows.Count > 0 Then
'Bind your data here
Else
'Display message here
Me.lblError.Text = "No Records found"
End If

macker
07-25-2007, 08:47 AM
Thanks for the advice.

The code was a little different, however, you put me in the right direction. I knew there was something like that because I had used it on a GridView before, but I could not remember what it was for the Datagrid.

As I said, the code is a little different, so I will post it here just incase anyone else wants to look.

Me.RecruitGrid.DataSource = DataMgr.GetExistingRecruits(lname, sorter, [Global].ConnectionString)
RecruitGrid.DataBind()
If RecruitGrid.Items.Count > 0 Then

Thanks again!

lmf232s
07-25-2007, 12:07 PM
We also looked at different things. Just to explain both:

The code I posted looked to see if any records were returned from the dataset. If there were records then the data was bound.

In your code you bound your data first and then checked to see if there were any items with in the control.

While they both have the same end result (Seeing if there is any data) there may be times when you dont want to bind to your control unless you have a dataset that contains records.

Thats it

Ribeyed
07-25-2007, 07:04 PM
Hi m8,
you can do a row count on your returned dataset then do an if statement with that or you can keep ur code and just show or hide ur datagrid accordinly.


Me.RecruitGrid.DataSource = DataMgr.GetExistingRecruits(lname, sorter, [Global].ConnectionString)

If RecruitGrid.Items.Count > 0 Then
RecruitGrid.DataBind()
End if


or


Me.RecruitGrid.DataSource = DataMgr.GetExistingRecruits(lname, sorter, [Global].ConnectionString)
RecruitGrid.DataBind()
If RecruitGrid.Items.Count > 0 Then
datagrid.visible = true
else
datagrid.visible = false


hope this helps m8

SK-BL-itz
02-21-2008, 08:14 AM
Hi David

Can you help a newbie in Bolton?

Chris

Ribeyed
02-21-2008, 12:11 PM
sure whats your question?

SK-BL-itz
02-21-2008, 03:11 PM
It's Chris - Sudden Strike and Cossacks! :)

Are you still playing?

Chris

Ribeyed
02-21-2008, 07:04 PM
wow! hi m8 send me a pm with your email address or something :D

SK-BL-itz
02-22-2008, 03:00 AM
Hi David - This thing won't allow me to use a pm, although I've had the message confirming membership. You can view my (home-made!) website at: http://www.ctp-photo.co.uk and contact me from there...

Chris

SK-BL-itz
02-27-2008, 05:04 AM
Did you send message? I've been having problems with disappearing e-mails, so am not sure what's happened...