Click here to Skip to main content
15,878,945 members
Articles / Desktop Programming / MFC
Article

Binding MFC Grid to database.

Rate me:
Please Sign up or sign in to vote.
4.74/5 (12 votes)
8 Oct 20022 min read 192.7K   4.2K   63   39
This version of MFC grid works with database.

Sample Image - MFCGridDatabaseversion.jpg

Introduction

Last week I posted a new version of MFC Grid Control which worked with database; I've changed the source code,and sub class it from CGridCtrl, and also deleted some extra code. So this is the new version.

Creating this control is the same as MFC grid exept that when you want to define variable grid, you have to define it with CDbMFCGrid, not CGridCtrl. You have to import msado15.dll to your project too. In yout stdafx.h, you can write domething like this:

#import "E:\Program Files\Common Files\System\ado\msado15.dll" 
    no_namespace rename( "EOF", "adoEOF" )

Here is the rest of information about this grid:

I've tested this grid with most of SQLServer data types, so I think it will work with most of them except Image ,binary ,timestamp ,sql_variant, uniqueidentifier. The same thing was found for MS-Access database, but I don't think these caused a lot of trouble, because these are not very common types for displaying in grid. Maybe I'll add these features later to my grid. Also if somebody wants to do it, he/she can mail new version to me, and I'll update source code. After all it is not FULLY tested and maybe there are some bugs in it.

How to use?

Simple. Just after you create grid like the way it is said in MFC Grid article, give datasource and sqlstring to it, then use Bind() and FillGrid() functions like this:

//SQL datasource
//m_gridctrl.m_strDataSource =
//L"Provider=SQLOLEDB.1;Integrated Security=SSPI;
//Persist Security Info=False;
//Initial Catalog=Test;Data Source=mazy;
//Use Procedure for Prepare=1;Auto Translate=True;
//Packet Size=4096;Workstation ID=MAZY;Use Encryption for 
//Data=False;Tag with column collation when possible=True";
//access datasource



m_gridctrl.m_strDataSource = "Provider=Microsoft.Jet.OLEDB.4.0;"
    "Data Source=db1.mdb";

m_gridctrl.m_strQuery = "select * from Table1";

m_gridctrl.Bind();
m_gridctrl.FillGrid();

That's it. It reads datasource and fills up the grid. Then if the grid is not in readonly mode, if user edits a cell, it will automaticlly update database; also if you select row(s) and press DELETE key, it will delete records from both database and grid.

Features

  • For adding records,you have to first add a new row to the grid. (You have to add it to last row), then simply call AddNewRecord() function for grid. Don't forget that this function always reads last row of grid and adds it to database.
    //add new row to grid then :
    m_gridctrl.Refresh();
    m_gridctrl.AddNewRecord();	
  • It sorts rows when you click on fixed column based on cell types automatically.
  • For all date/time types, I convert them to "%d/ %m/ %Y" format and put them in the grid, if you want other date/time format, you HAVE TO edit codes in two place - first find this line in CDbMFCGrid::FillGrid(void) function, CString str1 = date.Format(_T("%d/ %m/ %Y")); and change it to your desired format, then correct the codes in CALLBACK CDbMFCGrid::pfnCellDateCompare function. It is for comparing two date/time value and sorting date/time columns. Clear?
  • You can filter grid easily too:
    m_gridctrl.pRecordset->Filter = "col1 ='Mazy'";
    
    m_gridctrl.FillGrid();
  • If you use SetItemText() in your program and you want to update database, you have to call CDbMFCGrid::UpdateCell(int iRow,int iCol) after SetItemText()
  • CDbMFCGrid::DeleteDataRow(int iRow) function is for deleting a row from grid AND database.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionReadOnly mode? Pin
Mike Eriksson14-Sep-06 3:47
Mike Eriksson14-Sep-06 3:47 
AnswerRe: ReadOnly mode? Found small bug! Pin
Mike Eriksson14-Sep-06 4:33
Mike Eriksson14-Sep-06 4:33 
GeneralRe: ReadOnly mode? Found small bug! Pin
julycy18-Jan-07 12:53
julycy18-Jan-07 12:53 
GeneralBug in CheckVariant Pin
Franck Magron1-Dec-05 14:02
Franck Magron1-Dec-05 14:02 
GeneralWorking Sample Project Pin
fwcetus22-Jul-05 17:31
fwcetus22-Jul-05 17:31 
QuestionHow to AddNewRecord Pin
babamlj3-Jan-05 14:11
babamlj3-Jan-05 14:11 
GeneralHi~ Thanks for your Source and I need help! Pin
player2244512-Aug-04 9:44
player2244512-Aug-04 9:44 
Generalhi~ what's the problem Pin
enoloo_don4-May-04 19:36
enoloo_don4-May-04 19:36 
GeneralRe: hi~ what's the problem Pin
Mazdak5-May-04 1:23
Mazdak5-May-04 1:23 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 3:58
enoloo_don8-May-04 3:58 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 4:03
enoloo_don8-May-04 4:03 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 4:05
enoloo_don8-May-04 4:05 
GeneralRe: hi~ what's the problem Pin
Mazdak8-May-04 8:34
Mazdak8-May-04 8:34 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 23:50
enoloo_don8-May-04 23:50 
Generalcompile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pin
jinlong25-Apr-04 5:54
jinlong25-Apr-04 5:54 
GeneralRe: compile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pin
Mazdak25-Apr-04 8:44
Mazdak25-Apr-04 8:44 
GeneralExample Application Pin
garak16-Dec-03 4:38
garak16-Dec-03 4:38 
Hi,

does anybody of you have a link to an example application or maybe could send one to me via email (fpleyer76@gmx.de) with this DataGrid.
I tryed to include it to my CFORMVIEW Project, but get various compiling
errors. I work with VC7...

THX
Frank
GeneralRe: Example Application Pin
Anonymous23-Dec-03 19:35
Anonymous23-Dec-03 19:35 
GeneralRe: Example Application Pin
fwcetus2-May-05 9:50
fwcetus2-May-05 9:50 
GeneralCell not receiving focus Pin
Zenb Klen15-Jun-03 8:46
Zenb Klen15-Jun-03 8:46 
GeneralRe: Cell not receiving focus Pin
Mazdak15-Jun-03 20:55
Mazdak15-Jun-03 20:55 
GeneralRe: Cell not receiving focus Pin
guomaodou6-Sep-04 1:07
guomaodou6-Sep-04 1:07 
GeneralCompiler errors Pin
Marco Incalcaterra28-Nov-02 6:19
Marco Incalcaterra28-Nov-02 6:19 
GeneralRe: Compiler errors Pin
Mazdak28-Nov-02 8:17
Mazdak28-Nov-02 8:17 
GeneralRe: Compiler errors Pin
Marco Incalcaterra28-Nov-02 8:42
Marco Incalcaterra28-Nov-02 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.