Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > Datagrid, getting rid of a "null" row


Reply
 
Thread Tools Display Modes
  #1  
Old 02-26-2005, 09:44 AM
gilou28 gilou28 is offline
Centurion
 
Join Date: Feb 2004
Location: Canada
Posts: 120
Default Datagrid, getting rid of a "null" row


When the user scroll to the bottom of my datagrid, it creates a new row with null values. The user can then enter a new row.

I have a problem when the user open a new row and the leaves that row without entering values. In my currency manager, when the user changes row, I update the database. I fire a dg.update and a sqlda.update.

I've got an exception that is raised (dg.update) when the row is left with null values: IndexOutOfRangeException.

How can I get rid of that null row before firing the dg.update ?

Thanks
Reply With Quote
  #2  
Old 02-26-2005, 05:02 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Via the DataStyles object, you can assign various features to your dataGrid, including a .nulltext property. So, you can add an empty string instead, some other default value, and then, in the validating event, or when they leave the cell, you can trap this default value and act accordingly.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 02-28-2005, 05:11 AM
gilou28 gilou28 is offline
Centurion
 
Join Date: Feb 2004
Location: Canada
Posts: 120
Default

Quote:
Originally Posted by MKoslof
Via the DataStyles object, you can assign various features to your dataGrid, including a .nulltext property. So, you can add an empty string instead, some other default value, and then, in the validating event, or when they leave the cell, you can trap this default value and act accordingly.
Actually, it's not what I'm trying to fix... :-) I was not clear initially, I rephrase. :-)

In the DG, if the user "opens" a new row but do not enter anything, that empty row remains there and when I fire the dg.Update, the Update fails complaining about a rowindex of of bound. I want to delete that empty row. How do I do that.

Thanks again for your time.
Reply With Quote
  #4  
Old 02-28-2005, 04:32 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

You want to delete the empty row before updating? Why not use the .HasChanges and .GetChanges method, if the bound dataTable has no changes, nothing will be fired.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 03-01-2005, 05:48 AM
gilou28 gilou28 is offline
Centurion
 
Join Date: Feb 2004
Location: Canada
Posts: 120
Default

Quote:
Originally Posted by MKoslof
You want to delete the empty row before updating? Why not use the .HasChanges and .GetChanges method, if the bound dataTable has no changes, nothing will be fired.
The datasource of my dg is a table in a dataset.

In my case, when there is an empty row and I fire an dg.update, the aboved mentionned exception is raised.

The exception gets fired only when there is an empty row.

When the exception is fired, I have a big red X in place of a datagrid....

You mentionned the .HasChanged and .GetChanges.... but I'm using the .Update method only.... Is it either use the .Update and pray it's gonna work or do everything manually with the .HasChanged and .GetChanges ? :-)
Reply With Quote
  #6  
Old 03-01-2005, 05:16 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Before firing your update, you should check if the DataTable haschanges. Basically your dataGrid is bound to a dataset which contains the table and schema of the intial query. So if the user has not added a value record, there is nothing to update

Code:
Dim changes As DataTable changes = DirectCast(DataGrid1.DataSource, DataTable).GetChanges() If (Not changes Is Nothing) Then MessageBox.Show("User added something") End If

Basically you populate a datagrid. Then in the validating event, when the user leaves the dataGrid if they have tried to update an existing field (added text to a cell) or click on the bottom row in order to add a new record, the messagebox (in this case) will display. If they do nothing, nothing happens. if you need to trap the last cell the user was on, you will have to track the current cell. Then, if .GetChanges returns true, check that row..if there are null values prompt the user and flag it, etc.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 03-03-2005, 01:22 PM
gilou28 gilou28 is offline
Centurion
 
Join Date: Feb 2004
Location: Canada
Posts: 120
Default

Thanks, I'll try to put that in place over the weekend!
Reply With Quote
  #8  
Old 03-03-2005, 05:09 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Good luck, let us know if you have any more questions.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->