Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Are there simplified ways or controls that one can use to display records from database?
I usually manually create tables using LineTo and Textout functions. These approach is tedious.Are there easier ways like using controls or standard functions?
Posted
Comments
[no name] 7-Mar-13 12:30pm    
Yes... using a data grid springs to mind

1 solution

Yes there are. Create a new Dialog based MFC application using the Wizard.
Add a CDatabase and a CRecordset classes to your project.
The CDatabase class can then be linked to a single database, while each Table in your database is linked to each CRecordset class. You then need to add a control that will display the data. The most convinient way to display a contents of a database table would be using grid view. Please note that "table" can be one of your database's tables but also a query. You can attached a query like: "SELECT * FROM TABLE1 WHERE LASTNAME = 'Smith'" as your data source of your recordset.

See also: http://msdn.microsoft.com/en-us/library/czxt1e3z%28v=vs.71%29.aspx[^]

Now, regarding the control you will use to display the database contents, I recomend reading this article:
MFC Grid control 2.27[^]

Then, assuming your Grid is accessed via a member variable added to your dialog box, named m_gridctrl:

To link your grid control to an MS Access Database named "mydb", use this code:
C#
m_gridctrl.m_strDataSource = "Provider=Microsoft.Jet.OLEDB.4.0;"
    "Data Source=mydb.mdb";


To link your grid control to the actual table (recordset), named "MyTable" to be displayed, use this code:

C#
m_gridctrl.m_strQuery = "select * from MyTable";

m_gridctrl.Bind();
m_gridctrl.FillGrid();
 
Share this answer
 
Comments
Gbenbam 8-Mar-13 11:58am    
I have never created a dialog based application before. Will there be a main window or does it mean all windows will be dialogue boxes. I still need a main window as well as dialogue boxes. Do you think creating a dialogue based application will enable this?

Will the control work with on print too?
Michael Haephrati 8-Mar-13 12:00pm    
You can use SDI, MDI or Dialog based application. Use the Visual Studio wizard for that. You can do almost anything regardless of the model you are using. The implementation would be almost the same. Using a small dialog based application will help you learn how to implement this grid. When you are done, you can take your code (and your knowledge) to any other project

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900