Home All Groups Group Topic Archive Search About

DataGridViewCell has lost its DataGridView - How to solve?

Author
13 Nov 2006 12:31 AM
Daniel Manes
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.DataGridViewButtonCellAccessibleObject}
    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

Author
14 Nov 2006 2:41 AM
RobinS
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.
------------------
Show quoteHide quote
"Daniel Manes" <danth***@cox.net> wrote in message
news:1163377882.104364.321610@i42g2000cwa.googlegroups.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.DataGridViewButtonCellAccessibleObject}
>    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
>
>
Author
14 Nov 2006 9:23 PM
Daniel Manes
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:
Show quoteHide quote
> 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" <danth***@cox.net> wrote in message
> news:1163377882.104364.321610@i42g2000cwa.googlegroups.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.DataGridViewButtonCellAccessibleObject}
> >    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
> >
> >
Author
14 Nov 2006 10:59 PM
RobinS
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.
-----------------
Show quoteHide quote
"Daniel Manes" <danth***@cox.net> wrote in message
news:1163539399.540860.10720@i42g2000cwa.googlegroups.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" <danth***@cox.net> wrote in message
>> news:1163377882.104364.321610@i42g2000cwa.googlegroups.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.DataGridViewButtonCellAccessibleObject}
>> >    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
>> >
>> >
>