Windows Develop Bookmark and Share   
 index > Windows Forms General > window forms DataGridView in MFC Dilaog
 

window forms DataGridView in MFC Dilaog

Hi,

I have a window forms DataGridView in MFC Dilaog Which is used for accepting data from user.
DataGridView contains DataGridViewTextBoxColumns.

The problem is, the value of the cell that was last edited could not be retrieved.

Kindly help

Regards
Anjali Devi  Wednesday, September 23, 2009 10:36 AM
Hi Anjali Devi,

Have you called EndEdit method of DataGridView before you retrieve the data from cell?

I have use the following sample to host DataGridView into MFC project.
http://www.codeproject.com/KB/miscctrl/mfcdialogwinforms.aspx

After I host it, I can use it just like in Winform CLR C++ project. In order to test the issue, I have created one column in DataGridView in OnInitDialog method.
BOOL CDGVMFCDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

System::Windows::Forms::DataGridViewColumn^ dgvCol1 =
gcnew System::Windows::Forms::DataGridViewColumn(gcnew System::Windows::Forms::DataGridViewTextBoxCell());
dgvCol1->HeaderText = L"Column1";
dgvCol1->Name = L"Column1";
this->m_DataGridView1->Columns->Add(dgvCol1);


return TRUE; // return TRUE unless you set the focus to a control
}

When I retrieve the data, I use the following code.
void CDGVMFCDlg::OnBnClickedBtngetdata()
{
this->m_DataGridView1->EndEdit();
System::String^ msg =
gcnew System::String(this->m_DataGridView1->Rows[m_DataGridView1->Rows->Count - 2]->Cells[m_DataGridView1->Columns->Count - 1]->Value->ToString());
System::Windows::Forms::MessageBox::Show(msg);
}

The last cell in DataGridView is the one I am editing, I can retrieve the value from it.

Hope this helps.

Sincerely,
Kira Qian

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Friday, September 25, 2009 6:44 AM
Hi,
Post some code for understanding..
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 23, 2009 11:11 AM
Hi Anjali Devi,

Have you called EndEdit method of DataGridView before you retrieve the data from cell?

I have use the following sample to host DataGridView into MFC project.
http://www.codeproject.com/KB/miscctrl/mfcdialogwinforms.aspx

After I host it, I can use it just like in Winform CLR C++ project. In order to test the issue, I have created one column in DataGridView in OnInitDialog method.
BOOL CDGVMFCDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

System::Windows::Forms::DataGridViewColumn^ dgvCol1 =
gcnew System::Windows::Forms::DataGridViewColumn(gcnew System::Windows::Forms::DataGridViewTextBoxCell());
dgvCol1->HeaderText = L"Column1";
dgvCol1->Name = L"Column1";
this->m_DataGridView1->Columns->Add(dgvCol1);


return TRUE; // return TRUE unless you set the focus to a control
}

When I retrieve the data, I use the following code.
void CDGVMFCDlg::OnBnClickedBtngetdata()
{
this->m_DataGridView1->EndEdit();
System::String^ msg =
gcnew System::String(this->m_DataGridView1->Rows[m_DataGridView1->Rows->Count - 2]->Cells[m_DataGridView1->Columns->Count - 1]->Value->ToString());
System::Windows::Forms::MessageBox::Show(msg);
}

The last cell in DataGridView is the one I am editing, I can retrieve the value from it.

Hope this helps.

Sincerely,
Kira Qian

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  Friday, September 25, 2009 6:44 AM
Hi Kira,

Thank you very much for the timely help.
That realle solved my isseu.


As u had guessed I had nt called EndEdit method of DataGridView before you retrieve the data from cell

Once again Thank u

Regards,
Anjali

>>>>>>
>>>>>>
Hi Anjali Devi,

Have you called EndEdit method of DataGridView before you retrieve the data from cell?

I have use the following sample to host DataGridView into MFC project.
http://www.codeproject.com/KB/miscctrl/mfcdialogwinforms.aspx

After I host it, I can use it just like in Winform CLR C++ project. In order to test the issue, I have created one column in DataGridView in OnInitDialog method.
BOOL CDGVMFCDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
Anjali Devi  Wednesday, October 07, 2009 5:12 AM

You can use google to search for other answers

Custom Search

More Threads

• Suggestions for a server-client architecture
• vScrollBar A Panel?
• No FindString method in ListView! What to do?
• FlowLayoutPanel not shrinking width-wise
• can i embed html text formatting in a textbox?
• How to reuse a button ??Whats the logic behaind that?
• Form to "dock" to Windows desktop ie Ticker/Office Toolbar
• AutoScaleMode::Font and FormWindowState::Maximized
• Ctrl hotkeys
• Error Handling when calling a webservice