Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView Problem

Status
Not open for further replies.

DarkConsultant

Programmer
Dec 4, 2007
156
GB
Hi All,

I am trying to use a DataGridView which is bound to a list of DataObjects. The only problem I am having is that if I edit a cell to nothing (remove the cell contents) then on cell leave I get an uncatchable error. The error message is 'Object reference not set to an instance of an object' I am guessing that this refers to the cell but can get no further. I do not seem to be able to catch this error anywhere.

And yes I am leaving a space in each cell to get past the error but this is causing me problems further down the line.

Any ideas anyone.

Regards

DarkConsultant

Live long and prosper \\//
 
Hmmmm...

I found the answer ... well if not the answer then a solution.

Open DefaultCellStyle and set the Null value to a single space (which is not there the next time you look) and voila you have to answer your own query.

Its a mad world.

Regs

DarkConsultant

Live long and prosper \\//
 
Could you show us the code that is returning the error? We be able to come up with a better solution for you.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Thanks ousoonerjoe,

There is no code to demonstrate the error with.

This morning I cannot reproduce the error but am now getting 5 consecutive errors while setting a connection string. But the command executes fine ...

Have just booted dev pc and error has gone.

Ran virus check nada.

I think I will reinstall OS and vb 2005 as this is nuts.

Kindest regards

DarkConsultant

Live long and prosper \\//
 
Hi ousoonerjoe,

New OS new problem, I think!

Code is
With dgLicences
If .SelectedRows.Count = 1 Then
.SelectedRows(0).Cells(7).Value = False
.SelectedRows(0).Cells(8).Value = False
.SelectedRows(0).Cells(9).Value = False
.SelectedRows(0).Cells(10).Value = False
.SelectedRows(0).Cells(11).Value = False
.SelectedRows(0).Cells(CInt(WhichType)).Value = True
End If
End With

Where dgLicences is a datagridview and Cells 7 thru 11 are checkboxes and WhichType converts to an integer 7 - 11.

I am trying to uncheck all but WhichType, I originally tried

.SelectedRows(0).Cells(??).Value = (WhichType = ??)

but this gave me the same results ...

The results are bizarre the correct checkbox is checked but randomly (I can run the same code over and over and get different results) some checkboxes that were supposed to be unchecked are checked.

HELP


DarkConsultant

Live long and prosper \\//
 
Have you tried setting a default value of FALSE on those fields. I suspect it is returning DbNull or Nothing. The default value will clear this up. I've had the same issue with DbNull causing the "Object reference not set to an instance of an object" error in the past.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
If you open the Column Editor for the specific grid you'll see a property called DefaultCellStyle. Click on the <...> button and it will open the CellStyle Builder. Set the NullValue to FALSE.

I try to set a default value for all columns in my grids. When it isn't practical to do so, I have to check for Nothing or DbNull in code to prevent the object undefined error.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
Hi Mate,

No difference still getting random checked boxes as before.

Any ideas?

From the example I am attempting to set checkbox in column 7 and uncheck all others and have set up messageboxes to let me know the values ...

On running I get 5 messageboxes saying values 7 - 11 are now false and then one saying cb 7 is true - perfect, but the display shows cb 7, 8 and 10 are checked.

On rerunning I get all messageboxes as before but this time cb7 and 8 checked.

Arrrrgggghhhhhhhhhh!! I think I pulled a nail out while climbing the wall......

I have just created identical situation on an XP PC and exactly the same result.

Any thoughts?

DarkConsultant

Live long and prosper \\//
 
It really sounds to me that you are getting a chain of events that is changing the .Value on you. Without actually having the code in front of me, I'm not sure what more to do, other than step through it line my line and make sure when you set the .Value = FALSE that a series of Change, Validate, Leave, etc. events are firing and cascading to ultimately wipe or change the values assigned.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 

Instead of True and False, try using CheckState.Checked and CheckState.Unchecked

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi,

Tried that before, throws error saying the value needs to be boolean.

Mate I am gonna write this one off as a bad job. I have never worked well with bound controls, if I unbind this and load/save manually I dont get this error.

Thanks for all your work, I really appreciate it.

Kindest regards

DarkConsultant

Live long and prosper \\//
 
There is one other thing I've found that can cause that error. Does the Data Type of the column exactly match the Data Type of the field populating it? For example, a TINYINT does not match an INT.

Other than that, wish could have done more.

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 

if I unbind this and load/save manually I dont get this error.

This is exactly why I rarely use bound controls.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi ousoonerjoe,

Yah, they are all booleans. Mate dont beat yourself up, probably like yourself part of the fun is in the hunt ... I have unbound (is that a word) the data and all is sweet.

jebenson as usual you've made my day. I have always had problems with bound controls and go miles out of my way not to use them, all the time thinking its me thats the problem cos everyone else seems to use them fine ...

Has anyone else noticed that they get more work done during Wimbledon .....

Laters

P.S. My latest project is called Zat'nikit'el am I the only saddo that knows from whence this came?

DarkConsultant

Live long and prosper \\//
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top