A problem with using DataGridView control in an MFC CDialog

G

Guest

I have a very simple CDialog-based application (VC++ 8.0/2005), where a
DataGridView control is included through the usage of CWinFormsControl class.
I experience the following problem with this application. If I switch to a
different application (Alt+Tab, for example) while a grid cell is remaining
in editing mode and then go back to my application, the application hangs
(stops responding).

The problem can be reproduced using CWinFormsControl and CWinFormsView
classes, but it is NOT reproducible when using CFrameWnd and CWinFormsView
classes or when using DataGrid control instead of the new DataGridView.

Any ideas what can be the reason of the problem?

P.S.:
If anybody is interested, I can send you a very simple test-application
using which the problem can be easily reproduced.
 
P

programmer42

Hi Maksim,

I'm having a problem replicating your app. Did you any have problems
integrating the DataGridView into the CWinFormsControl< > template?
Because I'm having a problem using the DataGrid/DataGridView within a
MFC dialog.

I created a simple CDialog app (VC++ 8), created a DataSet from an Xml
file and then set it to the DataSource (setting the appropriate table
name as the DataMember), but nothing is displayed in the grid view. I
tried various approaches and data binding options, but nothing seems to
work.

I have no problem setting this up in WinForms - it just won't work in
MFC. I know the data's there because I can actually step right into it
from the DataGrid object (debugger) - it just won't display - the
ColumnCount is still 0.

What's the problem? Did you have this problem?

I'd love to help you, but I can't get past this problem.
 
G

Guest

I had the same problem. Unfortunately, I didn's succeed to understand the
cause of the problem, but I was able to find a workaround for solving it: I
wrapped the DataGridView control in a user control (a control derived from
UserControl class) and then embedded this user control into CWinFormsControl.

My main problem, described in the original post, can be reproduced even
without assigning any data source to the DataGridView and it doesn't depend
 
G

Guest

I've got the explanation and solution for both of these problems from
Microsoft Developer Support:

1) When the DataGridView is not data-bound and the user has set the focus to
edit one of the cell’s in the DataGridView and alt-tabs away from the
application and then back, the application hangs. You would like to know why
this is hanging and how to avoid it.

- This will reproduce with either the data-bound or non-data-bound control,
so long as there is an editable field to accept the keyboard focus. The
problem occurs when the Dialog attempting to find the default button when it
is activated. This search goes into an infinite loop. It starts with the
control with the focus (the edit control inside the DataGridView’s cell) and
searches until it either finds a control which responds to WM_GETDLGCODE with
DLGC_DEFPUSHBUTTON or it gets back to the control it started on. The problem
is that the DataGridView is not marked as containing focusable child controls
(with the WS_EX_CONTROLPARENT style), and so the dialog doesn’t search into
the DataGridView. Since it never searches in the DataGridView it doesn’t
find the edit control it started on and never finishes the loop. If you
watch the application in Spy++ you will see that it is not completely locked
but is forever receiving WM_GETDLGCODE messages. This can be fixed by adding
the WS_EX_CONTROLPARENT style to the DataGridView. This can be done external
to the DataGridView by sinking the HandleCreated event or within the
DataGridView by inheriting and overriding CreateParams to add the style. It
is essentially the same problem as is documented for CPropertySheets in
Q149501.
http://msdn.microsoft.com/library/e...WindowsFormsControlClassCreateParamsTopic.asp
http://msdn.microsoft.com/library/e...reference/dialogboxmessages/wm_getdlgcode.asp
http://msdn.microsoft.com/library/e...wreference/windowfunctions/createwindowex.asp
http://support.microsoft.com/?id=149501

2) When the DataGridView is bound to a DataTable in a DataSet the rows
and columns for that DataTable do not appear in the Grid.

- This is failing because data-binding requires that the bound control have
a BindingContext. Normally the DataGridView would use the BindingContext
belonging to the Form it is on, but since the test application’s DataGridView
is on an MFC dialog rather than on a Form it cannot use its parent’s context.
To use data-binding with the MFC-hosted DataGridView will require that the
application either create and manage the BindingContext on its own or host
the DataGridView inside a ContainerControl (such as a UserControl) which will
manage a binding context for its children. You can find more information
about BindingContexts at:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingcontext.aspx
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext.aspx
 

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