Click here to Skip to main content
11,927,711 members (31,936 online)
Click here to Skip to main content
Add your own
alternative version


62 bookmarked

Binding MFC Grid to database.

, 8 Oct 2002
Rate this:
Please Sign up or sign in to vote.
This version of MFC grid works with database.
<!-- Article image -->

Sample Image - MFCGridDatabaseversion.jpg

<!-- Add the rest of your HTML here -->


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

You may also be interested in...

Comments and Discussions

QuestionReadOnly mode? Pin
Mike Eriksson14-Sep-06 4:47
memberMike Eriksson14-Sep-06 4:47 
AnswerRe: ReadOnly mode? Found small bug! Pin
Mike Eriksson14-Sep-06 5:33
memberMike Eriksson14-Sep-06 5:33 
GeneralRe: ReadOnly mode? Found small bug! Pin
julycy18-Jan-07 13:53
memberjulycy18-Jan-07 13:53 
GeneralBug in CheckVariant Pin
Franck M1-Dec-05 15:02
memberFranck M1-Dec-05 15:02 
GeneralWorking Sample Project Pin
fwcetus22-Jul-05 18:31
memberfwcetus22-Jul-05 18:31 
QuestionHow to AddNewRecord Pin
babamlj3-Jan-05 15:11
memberbabamlj3-Jan-05 15:11 
GeneralHi~ Thanks for your Source and I need help! Pin
player2244512-Aug-04 10:44
memberplayer2244512-Aug-04 10:44 
Generalhi~ what's the problem Pin
yuanshangcao4-May-04 20:36
memberyuanshangcao4-May-04 20:36 
GeneralRe: hi~ what's the problem Pin
Mazdak5-May-04 2:23
memberMazdak5-May-04 2:23 
GeneralRe: hi~ what's the problem Pin
yuanshangcao8-May-04 4:58
memberyuanshangcao8-May-04 4:58 
GeneralRe: hi~ what's the problem Pin
yuanshangcao8-May-04 5:03
memberyuanshangcao8-May-04 5:03 
GeneralRe: hi~ what's the problem Pin
yuanshangcao8-May-04 5:05
memberyuanshangcao8-May-04 5:05 
GeneralRe: hi~ what's the problem Pin
Mazdak8-May-04 9:34
memberMazdak8-May-04 9:34 
GeneralRe: hi~ what's the problem Pin
yuanshangcao9-May-04 0:50
memberyuanshangcao9-May-04 0:50 
Generalcompile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pin
jinlong25-Apr-04 6:54
memberjinlong25-Apr-04 6:54 
GeneralRe: compile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pin
Mazdak25-Apr-04 9:44
memberMazdak25-Apr-04 9:44 
GeneralExample Application Pin
garak16-Dec-03 5:38
membergarak16-Dec-03 5:38 
GeneralRe: Example Application Pin
Anonymous23-Dec-03 20:35
sussAnonymous23-Dec-03 20:35 
GeneralRe: Example Application Pin
fwcetus2-May-05 10:50
memberfwcetus2-May-05 10:50 
GeneralCell not receiving focus Pin
Andre Perroud15-Jun-03 9:46
memberAndre Perroud15-Jun-03 9:46 
GeneralRe: Cell not receiving focus Pin
Mazdak15-Jun-03 21:55
memberMazdak15-Jun-03 21:55 
GeneralRe: Cell not receiving focus Pin
guomaodou6-Sep-04 2:07
memberguomaodou6-Sep-04 2:07 
GeneralCompiler errors Pin
Marco Incalcaterra28-Nov-02 7:19
memberMarco Incalcaterra28-Nov-02 7:19 
GeneralRe: Compiler errors Pin
Mazdak28-Nov-02 9:17
memberMazdak28-Nov-02 9:17 
GeneralRe: Compiler errors Pin
Marco Incalcaterra28-Nov-02 9:42
memberMarco Incalcaterra28-Nov-02 9: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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 9 Oct 2002
Article Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid