Click here to Skip to main content
Click here to Skip to main content
Go to top

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

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

Share

About the Author

Mazdak
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
QuestionReadOnly mode? PinmemberMike Eriksson14-Sep-06 3:47 
AnswerRe: ReadOnly mode? Found small bug! PinmemberMike Eriksson14-Sep-06 4:33 
GeneralRe: ReadOnly mode? Found small bug! Pinmemberjulycy18-Jan-07 12:53 
GeneralBug in CheckVariant PinmemberFranck M1-Dec-05 14:02 
GeneralWorking Sample Project Pinmemberfwcetus22-Jul-05 17:31 
QuestionHow to AddNewRecord Pinmemberbabamlj3-Jan-05 14:11 
GeneralHi~ Thanks for your Source and I need help! Pinmemberplayer2244512-Aug-04 9:44 
Generalhi~ what's the problem Pinmemberyuanshangcao4-May-04 19:36 
GeneralRe: hi~ what's the problem PinmemberMazdak5-May-04 1:23 
GeneralRe: hi~ what's the problem Pinmemberyuanshangcao8-May-04 3:58 
GeneralRe: hi~ what's the problem Pinmemberyuanshangcao8-May-04 4:03 
GeneralRe: hi~ what's the problem Pinmemberyuanshangcao8-May-04 4:05 
GeneralRe: hi~ what's the problem PinmemberMazdak8-May-04 8:34 
GeneralRe: hi~ what's the problem Pinmemberyuanshangcao8-May-04 23:50 
Generalcompile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory Pinmemberjinlong25-Apr-04 5:54 
GeneralRe: compile message:Cannot open include file: 'GridCellNumeric.h': No such file or directory PinmemberMazdak25-Apr-04 8:44 
GeneralExample Application Pinmembergarak16-Dec-03 4:38 
GeneralRe: Example Application PinsussAnonymous23-Dec-03 19:35 
GeneralRe: Example Application Pinmemberfwcetus2-May-05 9:50 
GeneralCell not receiving focus PinmemberAndre Perroud15-Jun-03 8:46 
GeneralRe: Cell not receiving focus PinmemberMazdak15-Jun-03 20:55 
GeneralRe: Cell not receiving focus Pinmemberguomaodou6-Sep-04 1:07 
GeneralCompiler errors PinmemberMarco Incalcaterra28-Nov-02 6:19 
GeneralRe: Compiler errors PinmemberMazdak28-Nov-02 8:17 
GeneralRe: Compiler errors PinmemberMarco Incalcaterra28-Nov-02 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140916.1 | Last Updated 9 Oct 2002
Article Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid