473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DataGridViewCell has lost its DataGridView - How to solve?

I need a strategy to debug this situation...

I can't put all the code involved, but here are some of the critical
lines with comments:

-------------------------
Private _parentDataCell As DataGridViewCell 'declare private field
_parentDataCell = _parentDataGrid.Rows(rowIndex).Cells(columnIndex)
'set to a specific cell
Debug.Print(_parentDataCell.DataGridView.ToString) 'prints:
System.Windows.Forms.DataGridView
_childDialogResult = _childDialog.ShowDialog() 'show a dialog, user
does certain stuff with dialog
Debug.Print(_parentTableName & " " &
_parentDataCell.DataGridView.ToString) 'throws NullReferenceException
-------------------------

So, the problem is that, somehow, while the user is doing stuff with
the dialog that gets opened, _parentDataCell gets "disembodied"--it's
no longer pointing to a DataGridView. If I do "? _parentDataCell" in
the Immediate window, I get the following:

-------------------------
? _parentDataCell
{System.Windows.Forms.DataGridViewButtonCell}
System.Windows.Forms.DataGridViewButtonCell:
{System.Windows.Forms.DataGridViewButtonCell}
AccessibilityObject:
{System.Windows.Forms.DataGridViewButtonCell.DataG ridViewButtonCellAccessibleObject}
ColumnIndex: 2
ContentBounds: {X = 0 Y = 0 Width = 0 Height = 0}
ContextMenuStrip: Nothing
DataGridView: Nothing
DefaultNewRowValue: Nothing
Displayed: False
EditedFormattedValue: Nothing
EditType: Nothing
ErrorIconBounds: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
ErrorText: ""
FormattedValue: Nothing
FormattedValueType: {Name = "String" FullName = "System.String"}
Frozen: False
HasStyle: False
InheritedState: 80
InheritedStyle: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
IsInEditMode: False
OwningColumn: {System.Windows.Forms.DataGridViewButtonColumn}
OwningRow: {System.Windows.Forms.DataGridViewRow}
PreferredSize: {Width = -1 Height = -1}
ReadOnly: False
Resizable: False
RowIndex: -1
Selected: False
Size: {Width = -1 Height = -1}
State: None {0}
Style: {System.Windows.Forms.DataGridViewCellStyle}
Tag: Nothing
ToolTipText: ""
Value: Nothing
ValueType: {Name = "String" FullName = "System.String"}
Visible: True
-------------------------

Notice that _parentDataCell is not "Nothing", but it no longer has a
DataGridView, it's dimensions are zero, it's not displayed, etc..
Interestingly, it still has a ColumnIndex of 2, but it's RowIndex is
now -1.

I can't find anything in my code that would be causing this, but
obviously something is. What I need is some way to track or trace
_parentDataCell over time so I can see exactly when it "loses its
identity." Then maybe I'll have some hope of figuring out what's going
on.

How do I do this in Visual Studio 2005?

Any other tips/advice?

Thanks in advance,

-Dan

Nov 13 '06 #1
3 4609
The easiest thing to do is add the line where you
set _parentDataCell right before you use it again.
But I understand why you would want to know where
it's getting changed.

I thought there was a way to break in debug mode
when the value of a variable changed, but I can't
find it.

So I'd put a breakpoint where you first set
_parentDataCell. When you get there and it stops,
right-click on it and add a Watch on it. Then step
through your code, and watch it in the Watch
window and see when it changes.

It could be that if you are changing rowIndex
and/or columnIndex somewhere, it is redefining
parentDataCell to point at that new location,
although I think that would be unexpected.

Robin S.
------------------
"Daniel Manes" <da******@cox.netwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>I need a strategy to debug this situation...

I can't put all the code involved, but here are some of the critical
lines with comments:

-------------------------
Private _parentDataCell As DataGridViewCell 'declare private field
_parentDataCell = _parentDataGrid.Rows(rowIndex).Cells(columnIndex)
'set to a specific cell
Debug.Print(_parentDataCell.DataGridView.ToString) 'prints:
System.Windows.Forms.DataGridView
_childDialogResult = _childDialog.ShowDialog() 'show a dialog, user
does certain stuff with dialog
Debug.Print(_parentTableName & " " &
_parentDataCell.DataGridView.ToString) 'throws NullReferenceException
-------------------------

So, the problem is that, somehow, while the user is doing stuff with
the dialog that gets opened, _parentDataCell gets "disembodied"--it's
no longer pointing to a DataGridView. If I do "? _parentDataCell" in
the Immediate window, I get the following:

-------------------------
? _parentDataCell
{System.Windows.Forms.DataGridViewButtonCell}
System.Windows.Forms.DataGridViewButtonCell:
{System.Windows.Forms.DataGridViewButtonCell}
AccessibilityObject:
{System.Windows.Forms.DataGridViewButtonCell.DataG ridViewButtonCellAccessibleObject}
ColumnIndex: 2
ContentBounds: {X = 0 Y = 0 Width = 0 Height = 0}
ContextMenuStrip: Nothing
DataGridView: Nothing
DefaultNewRowValue: Nothing
Displayed: False
EditedFormattedValue: Nothing
EditType: Nothing
ErrorIconBounds: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
ErrorText: ""
FormattedValue: Nothing
FormattedValueType: {Name = "String" FullName = "System.String"}
Frozen: False
HasStyle: False
InheritedState: 80
InheritedStyle: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
IsInEditMode: False
OwningColumn: {System.Windows.Forms.DataGridViewButtonColumn}
OwningRow: {System.Windows.Forms.DataGridViewRow}
PreferredSize: {Width = -1 Height = -1}
ReadOnly: False
Resizable: False
RowIndex: -1
Selected: False
Size: {Width = -1 Height = -1}
State: None {0}
Style: {System.Windows.Forms.DataGridViewCellStyle}
Tag: Nothing
ToolTipText: ""
Value: Nothing
ValueType: {Name = "String" FullName = "System.String"}
Visible: True
-------------------------

Notice that _parentDataCell is not "Nothing", but it no longer has a
DataGridView, it's dimensions are zero, it's not displayed, etc..
Interestingly, it still has a ColumnIndex of 2, but it's RowIndex is
now -1.

I can't find anything in my code that would be causing this, but
obviously something is. What I need is some way to track or trace
_parentDataCell over time so I can see exactly when it "loses its
identity." Then maybe I'll have some hope of figuring out what's going
on.

How do I do this in Visual Studio 2005?

Any other tips/advice?

Thanks in advance,

-Dan


Nov 14 '06 #2
Hey Robin,

Thanks for the reply. I actually managed to solved the problem, but I
still wonder if there would have been an easier way. Some comments
below.
So I'd put a breakpoint where you first set
_parentDataCell. When you get there and it stops,
right-click on it and add a Watch on it. Then step
through your code, and watch it in the Watch
window and see when it changes.
Good idea. Actually what I tried :) But here's the problem: You can
only step through the debugger until all code associated with an event
is run. Then the debugger just stops. It wasn't until several mouse
clicks later that the link was getting lost.

So, I thought: put a break point in click event and keep watching the
value. But there was a problem with that too. The event handlers are in
a different class than _parentDataCell, so _parentDataCell is out of
scope (i.e., you can't see its value in the debugger/watcher).

Also, new instances of the class containing _parentDataCell get created
as the user clicks various buttons, so, how would the watcher know
which _parentDataCell I was talking about anyway?

The only solution I could think of was to create a shared (static)
variable called _firstParentDataCell, and set it equal to
_parentDataCell only if _parentDataCell had not yet been set. Then I
wrote a little shared function that tests to see whether
_firstParentDataCell is still "intact." Finally, I placed calls to the
function strategically through the code to see when the data was
getting lost.

This worked but it sure seemed like a lot of hassle. Turned out, the
bug was caused by one of those user clicks causing the filter on the
original DataGridView to get reapplied. The cell in question was still
there, but the act of refiltering caused all references to go blooey.

So, yeah, programming is hard :)

-Dan

RobinS wrote:
The easiest thing to do is add the line where you
set _parentDataCell right before you use it again.
But I understand why you would want to know where
it's getting changed.

I thought there was a way to break in debug mode
when the value of a variable changed, but I can't
find it.

So I'd put a breakpoint where you first set
_parentDataCell. When you get there and it stops,
right-click on it and add a Watch on it. Then step
through your code, and watch it in the Watch
window and see when it changes.

It could be that if you are changing rowIndex
and/or columnIndex somewhere, it is redefining
parentDataCell to point at that new location,
although I think that would be unexpected.

Robin S.
------------------
"Daniel Manes" <da******@cox.netwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I need a strategy to debug this situation...

I can't put all the code involved, but here are some of the critical
lines with comments:

-------------------------
Private _parentDataCell As DataGridViewCell 'declare private field
_parentDataCell = _parentDataGrid.Rows(rowIndex).Cells(columnIndex)
'set to a specific cell
Debug.Print(_parentDataCell.DataGridView.ToString) 'prints:
System.Windows.Forms.DataGridView
_childDialogResult = _childDialog.ShowDialog() 'show a dialog, user
does certain stuff with dialog
Debug.Print(_parentTableName & " " &
_parentDataCell.DataGridView.ToString) 'throws NullReferenceException
-------------------------

So, the problem is that, somehow, while the user is doing stuff with
the dialog that gets opened, _parentDataCell gets "disembodied"--it's
no longer pointing to a DataGridView. If I do "? _parentDataCell" in
the Immediate window, I get the following:

-------------------------
? _parentDataCell
{System.Windows.Forms.DataGridViewButtonCell}
System.Windows.Forms.DataGridViewButtonCell:
{System.Windows.Forms.DataGridViewButtonCell}
AccessibilityObject:
{System.Windows.Forms.DataGridViewButtonCell.DataG ridViewButtonCellAccessibleObject}
ColumnIndex: 2
ContentBounds: {X = 0 Y = 0 Width = 0 Height = 0}
ContextMenuStrip: Nothing
DataGridView: Nothing
DefaultNewRowValue: Nothing
Displayed: False
EditedFormattedValue: Nothing
EditType: Nothing
ErrorIconBounds: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
ErrorText: ""
FormattedValue: Nothing
FormattedValueType: {Name = "String" FullName = "System.String"}
Frozen: False
HasStyle: False
InheritedState: 80
InheritedStyle: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
IsInEditMode: False
OwningColumn: {System.Windows.Forms.DataGridViewButtonColumn}
OwningRow: {System.Windows.Forms.DataGridViewRow}
PreferredSize: {Width = -1 Height = -1}
ReadOnly: False
Resizable: False
RowIndex: -1
Selected: False
Size: {Width = -1 Height = -1}
State: None {0}
Style: {System.Windows.Forms.DataGridViewCellStyle}
Tag: Nothing
ToolTipText: ""
Value: Nothing
ValueType: {Name = "String" FullName = "System.String"}
Visible: True
-------------------------

Notice that _parentDataCell is not "Nothing", but it no longer has a
DataGridView, it's dimensions are zero, it's not displayed, etc..
Interestingly, it still has a ColumnIndex of 2, but it's RowIndex is
now -1.

I can't find anything in my code that would be causing this, but
obviously something is. What I need is some way to track or trace
_parentDataCell over time so I can see exactly when it "loses its
identity." Then maybe I'll have some hope of figuring out what's going
on.

How do I do this in Visual Studio 2005?

Any other tips/advice?

Thanks in advance,

-Dan
Nov 14 '06 #3
It's cool that you figured out what the problem was.
You could also have set ParentDataCell as a property,
but that has the same impact as your solution.

If programming wasn't hard, then anybody could do it,
and it wouldn't be fun any more.

Robin S.
-----------------
"Daniel Manes" <da******@cox.netwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Hey Robin,

Thanks for the reply. I actually managed to solved the problem, but I
still wonder if there would have been an easier way. Some comments
below.
>So I'd put a breakpoint where you first set
_parentDataCell. When you get there and it stops,
right-click on it and add a Watch on it. Then step
through your code, and watch it in the Watch
window and see when it changes.

Good idea. Actually what I tried :) But here's the problem: You can
only step through the debugger until all code associated with an event
is run. Then the debugger just stops. It wasn't until several mouse
clicks later that the link was getting lost.

So, I thought: put a break point in click event and keep watching the
value. But there was a problem with that too. The event handlers are in
a different class than _parentDataCell, so _parentDataCell is out of
scope (i.e., you can't see its value in the debugger/watcher).

Also, new instances of the class containing _parentDataCell get created
as the user clicks various buttons, so, how would the watcher know
which _parentDataCell I was talking about anyway?

The only solution I could think of was to create a shared (static)
variable called _firstParentDataCell, and set it equal to
_parentDataCell only if _parentDataCell had not yet been set. Then I
wrote a little shared function that tests to see whether
_firstParentDataCell is still "intact." Finally, I placed calls to the
function strategically through the code to see when the data was
getting lost.

This worked but it sure seemed like a lot of hassle. Turned out, the
bug was caused by one of those user clicks causing the filter on the
original DataGridView to get reapplied. The cell in question was still
there, but the act of refiltering caused all references to go blooey.

So, yeah, programming is hard :)

-Dan

RobinS wrote:
>The easiest thing to do is add the line where you
set _parentDataCell right before you use it again.
But I understand why you would want to know where
it's getting changed.

I thought there was a way to break in debug mode
when the value of a variable changed, but I can't
find it.

So I'd put a breakpoint where you first set
_parentDataCell. When you get there and it stops,
right-click on it and add a Watch on it. Then step
through your code, and watch it in the Watch
window and see when it changes.

It could be that if you are changing rowIndex
and/or columnIndex somewhere, it is redefining
parentDataCell to point at that new location,
although I think that would be unexpected.

Robin S.
------------------
"Daniel Manes" <da******@cox.netwrote in message
news:11**********************@i42g2000cwa.googleg roups.com...
>I need a strategy to debug this situation...

I can't put all the code involved, but here are some of the critical
lines with comments:

-------------------------
Private _parentDataCell As DataGridViewCell 'declare private field
_parentDataCell = _parentDataGrid.Rows(rowIndex).Cells(columnIndex)
'set to a specific cell
Debug.Print(_parentDataCell.DataGridView.ToString) 'prints:
System.Windows.Forms.DataGridView
_childDialogResult = _childDialog.ShowDialog() 'show a dialog, user
does certain stuff with dialog
Debug.Print(_parentTableName & " " &
_parentDataCell.DataGridView.ToString) 'throws NullReferenceException
-------------------------

So, the problem is that, somehow, while the user is doing stuff with
the dialog that gets opened, _parentDataCell gets "disembodied"--it's
no longer pointing to a DataGridView. If I do "? _parentDataCell" in
the Immediate window, I get the following:

-------------------------
? _parentDataCell
{System.Windows.Forms.DataGridViewButtonCell}
System.Windows.Forms.DataGridViewButtonCell:
{System.Windows.Forms.DataGridViewButtonCell}
AccessibilityObject:
{System.Windows.Forms.DataGridViewButtonCell.DataG ridViewButtonCellAccessibleObject}
ColumnIndex: 2
ContentBounds: {X = 0 Y = 0 Width = 0 Height = 0}
ContextMenuStrip: Nothing
DataGridView: Nothing
DefaultNewRowValue: Nothing
Displayed: False
EditedFormattedValue: Nothing
EditType: Nothing
ErrorIconBounds: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
ErrorText: ""
FormattedValue: Nothing
FormattedValueType: {Name = "String" FullName = "System.String"}
Frozen: False
HasStyle: False
InheritedState: 80
InheritedStyle: {"Cell is not in a DataGridView. The cell cannot
retrieve the inherited cell style."}
IsInEditMode: False
OwningColumn: {System.Windows.Forms.DataGridViewButtonColumn}
OwningRow: {System.Windows.Forms.DataGridViewRow}
PreferredSize: {Width = -1 Height = -1}
ReadOnly: False
Resizable: False
RowIndex: -1
Selected: False
Size: {Width = -1 Height = -1}
State: None {0}
Style: {System.Windows.Forms.DataGridViewCellStyle}
Tag: Nothing
ToolTipText: ""
Value: Nothing
ValueType: {Name = "String" FullName = "System.String"}
Visible: True
-------------------------

Notice that _parentDataCell is not "Nothing", but it no longer has a
DataGridView, it's dimensions are zero, it's not displayed, etc..
Interestingly, it still has a ColumnIndex of 2, but it's RowIndex is
now -1.

I can't find anything in my code that would be causing this, but
obviously something is. What I need is some way to track or trace
_parentDataCell over time so I can see exactly when it "loses its
identity." Then maybe I'll have some hope of figuring out what's going
on.

How do I do this in Visual Studio 2005?

Any other tips/advice?

Thanks in advance,

-Dan


Nov 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: 2G | last post by:
Hi, Is there a way to set a cell in a DataGridView invisible? the property in readonly. I have some columns with checkboxes but there are some rows that should not have the checkbox available...
2
by: joey.powell | last post by:
Does anyone know how to access, with code, combo boxes in datagridview cells? I would like to be able to "load" them with values from say, a list of strings.
1
by: John J. Hughes II | last post by:
I am trying to create a custom DataGridViewCell which is "NOT" derived from DataGridViewTextBoxColumn. The painting works fine but I am unable to get the control into edit mode. Do I need to...
3
by: Daniel Manes | last post by:
I need a strategy to debug this situation... I can't put all the code involved, but here are some of the critical lines with comments: ------------------------- Private _parentDataCell As...
0
by: snowjim | last post by:
Hey! How do I clone a DataGridViewCell? I Have tested the fallowing: MyCostomRow row = r.Clone(); for (Int32 index = 0; index < r.Cells.Count; index++) { row.Cells =...
0
by: Spam Catcher | last post by:
Hi all, I'm trying to set the DataGridViewCell.Value of an unbound column. I can set the value in code, but the value isn't being displayed. I tried to RefreshEdit/EndEdit/Invalidate the...
0
by: Robert Koch | last post by:
Hello! I'm writing a Windows Application and want to host different User Controls in a DataGridView. The Controls are not known during design time, they could be graphs, comboboxes, curves,...
1
by: JB | last post by:
Hi Everyone, I have a DataGridView with Combo Boxes in its cells. I'd like to know if the user has clicked on the right side arrow button that triggers the opening of the drop down list. After...
0
by: JB | last post by:
Hi All, I have a DataGridView issue. One of the columns in my DataGridView control is a combo box. I want the user to be able to edit the value as well as choosing it from the drop down list....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.