Click here to Skip to main content
15,390,695 members
Articles / Desktop Programming / MFC
Posted 29 Sep 2002


64 bookmarked

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
This version of MFC grid works with database.

Sample Image - MFCGridDatabaseversion.jpg


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";


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.


  • 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 :
  • 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'";
  • 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.


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


About the Author

Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

Comments and Discussions

QuestionReadOnly mode? Pin
Mike Eriksson14-Sep-06 3:47
MemberMike Eriksson14-Sep-06 3:47 
AnswerRe: ReadOnly mode? Found small bug! Pin
Mike Eriksson14-Sep-06 4:33
MemberMike Eriksson14-Sep-06 4:33 
GeneralRe: ReadOnly mode? Found small bug! Pin
julycy18-Jan-07 12:53
Memberjulycy18-Jan-07 12:53 
GeneralBug in CheckVariant Pin
Franck Magron1-Dec-05 14:02
MemberFranck Magron1-Dec-05 14:02 
GeneralWorking Sample Project Pin
fwcetus22-Jul-05 17:31
Memberfwcetus22-Jul-05 17:31 
QuestionHow to AddNewRecord Pin
babamlj3-Jan-05 14:11
Memberbabamlj3-Jan-05 14:11 
GeneralHi~ Thanks for your Source and I need help! Pin
player2244512-Aug-04 9:44
Memberplayer2244512-Aug-04 9:44 
Generalhi~ what's the problem Pin
enoloo_don4-May-04 19:36
Memberenoloo_don4-May-04 19:36 
GeneralRe: hi~ what's the problem Pin
Mazdak5-May-04 1:23
MemberMazdak5-May-04 1:23 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 3:58
Memberenoloo_don8-May-04 3:58 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 4:03
Memberenoloo_don8-May-04 4:03 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 4:05
Memberenoloo_don8-May-04 4:05 
GeneralRe: hi~ what's the problem Pin
Mazdak8-May-04 8:34
MemberMazdak8-May-04 8:34 
GeneralRe: hi~ what's the problem Pin
enoloo_don8-May-04 23:50
Memberenoloo_don8-May-04 23:50 
Generalcompile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pin
jinlong25-Apr-04 5:54
Memberjinlong25-Apr-04 5:54 
GeneralRe: compile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pin
Mazdak25-Apr-04 8:44
MemberMazdak25-Apr-04 8:44 
GeneralExample Application Pin
garak16-Dec-03 4:38
Membergarak16-Dec-03 4:38 
GeneralRe: Example Application Pin
Anonymous23-Dec-03 19:35
MemberAnonymous23-Dec-03 19:35 
GeneralRe: Example Application Pin
fwcetus2-May-05 9:50
Memberfwcetus2-May-05 9:50 
GeneralCell not receiving focus Pin
Zenb Klen15-Jun-03 8:46
MemberZenb Klen15-Jun-03 8:46 
GeneralRe: Cell not receiving focus Pin
Mazdak15-Jun-03 20:55
MemberMazdak15-Jun-03 20:55 
GeneralRe: Cell not receiving focus Pin
guomaodou6-Sep-04 1:07
Memberguomaodou6-Sep-04 1:07 
GeneralCompiler errors Pin
Marco Incalcaterra28-Nov-02 6:19
MemberMarco Incalcaterra28-Nov-02 6:19 
GeneralRe: Compiler errors Pin
Mazdak28-Nov-02 8:17
MemberMazdak28-Nov-02 8:17 
GeneralRe: Compiler errors Pin
Marco Incalcaterra28-Nov-02 8:42
MemberMarco 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.