DataGridView Disappears

J

Jonathan Wood

Can anyone tell me what's happening here.

I have a Windows form with a docked DataGridView. The following handler
handles a click on a toolbar button:

private void toolRun_Click(object sender, EventArgs e)
{
using (DataGridView grd = customersDataGridView)
{
for (int i = 0; i < grd.Rows.Count; i++)
{
object o = grd["CustAmount", i].Value;
if (o != null)
MessageBox.Show(o.ToString());
o = grd["CustEmail", i].Value;
if (o != null)
MessageBox.Show(o.ToString());
}
}
}

It behaves exactly as expected, but when it's finished, the DataGridView
disappears from the form! I'm just left with a blank form with only my menu
and toolbars.

Any ideas?
 
J

Jonathan Wood

I guess I'm not understanding 'using' correctly. Now I think it disposes the
object when the block ends.

Can someone tell me the correct syntax when I want to reference something
like customersDataGridView... without typing the long name each time?

Thanks.

Jonathan
 
N

Nicholas Paldino [.NET/C# MVP]

Jonathan,

Well, as it should disappear. The using statement will call Dispose on
the item in the using part, and in this case, it is your grid. However,
your grid is still being used, so when the using statement exits, dispose is
called, and the window that contains the grid is destroyed.

In this case, if you want to use a shorter name for the grid, just
assign it to a variable:

DataGridView grd = customersDataGridView;

And then use grd.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jonathan Wood said:
I guess I'm not understanding 'using' correctly. Now I think it disposes
the object when the block ends.

Can someone tell me the correct syntax when I want to reference something
like customersDataGridView... without typing the long name each time?

Thanks.

Jonathan

Jonathan Wood said:
Can anyone tell me what's happening here.

I have a Windows form with a docked DataGridView. The following handler
handles a click on a toolbar button:

private void toolRun_Click(object sender, EventArgs e)
{
using (DataGridView grd = customersDataGridView)
{
for (int i = 0; i < grd.Rows.Count; i++)
{
object o = grd["CustAmount", i].Value;
if (o != null)
MessageBox.Show(o.ToString());
o = grd["CustEmail", i].Value;
if (o != null)
MessageBox.Show(o.ToString());
}
}
}

It behaves exactly as expected, but when it's finished, the DataGridView
disappears from the form! I'm just left with a blank form with only my
menu and toolbars.

Any ideas?
 
M

Marc Gravell

You are "using" it - this means it gets Dispose()d at the end.

"using" is very different to VBs "With"...

Just work with customersDataGridView directly - there is not need for
grd at all.

Marc
 
J

Jonathan Wood

Nicholas,
In this case, if you want to use a shorter name for the grid, just
assign it to a variable:

DataGridView grd = customersDataGridView;

Ah, yeah, I guess that's too easy. I still think in C++ and was thinking
about DataGridView& grd or something along those lines.

Thanks.

Jonathan
 
Joined
Sep 9, 2011
Messages
1
Reaction score
0
This is an old question

But I spend hours looking for the solution, so when i found the cause in my app.

I decided to still add my findings, maybe they help someone else with this problem.

My datagridview also kept disapearing from the form, several frustrating days later.

The problem seemed to be that i passed a variable to the form by declaring it in the designer code of the form, under InitializeComponent(ByVal CurrentRecordId As String)

And also declared in the form code.

Public Sub New(ByVal CurrentRecordId As String)
InitializeComponent(CurrentRecordId)
End Sub

For some reason this effects the datagridview, it disapears the moment you run the app, but the form with the datagridview still works.

But eventually caused unhandeld errors, solution was to not pass a variable from the first form, to the second form, but to use a global variable declared in a module.

Hope this help someone.

John van Eijck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top