A Very Useful Class to Display SQL SELECT Results | CodeGuru

A Very Useful Class to Display SQL SELECT Results

Figure 1. Illustrate CRecordListCtrl based on standard CListCtrl. Introduction In this article, I present a simple CRecordListCtrl to display the results of a SQL SELECT operation. Why another similar grid control? Yeah, you say; many grids are available for VC++ developers, but this control is small and very useful in MFC applications. Moreover, in combination […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 29, 2003
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Figure 1. Illustrate CRecordListCtrl based on standard CListCtrl.

Introduction

In this article, I present a simple CRecordListCtrl to display the results of a SQL SELECT operation. Why another similar grid control? Yeah, you say; many grids are available for VC++ developers, but this control is small and very useful in MFC applications. Moreover, in combination with CListCtrlEx, presented at http://www.creative-case.com/articles/ClistctrlEx.htm, you will find many advanced unique features for developers and end users. Readers also can replace MFC ODBC classes to MFC DAO. Using ADO or OLE DB classes is also possible. Moreover, a demo for CListCtrlEx virtual grid control targeted for a large amount of data was published in September of 2004 http://www.creative-case.com/articles/ClistctrlEx.htm.

Figure 2. Illustrate CRecordListCtrl based on CListCtrlEx.

In most cases, all that you need to do is call the following:

   // aDSN is ODBC datasource name
   // aSQL is SQL SELECT query
   m_List.LoadListCtrl(CString aDSN, CString aSQL);

How to Use the Control

Using CRecordListCtrl is very simple.

  1. Include the RecordListCtrlEx.h and RecordListCtrlEx.cpp files in your project.
  2. If you want to use CListCtrlEx in this control instead of CListCtrl, just replace CListCtrl with CListCtrlEx.

Conclusion

By reading this article, you receive two variants for using the CRecordListCtrl control. The first is based on CListCtrl, providing an easy-to-use way to display SQL SELECT query results. The second is based on CListCtrlEx, and also makes an easy way possible to show the results of SQL SELECT and provides a lot of end-user-friendly functionality. The functions are:

  • Multicolumn sorting (working as SQL ORDER BY)
  • Multicolumn filtering (working as an additional WHERE section in SQL SELECT)
  • Column data type auto detection (allows the user input to be more friendly)
  • Column coloring and dragging
  • Storing/Loading control state
Advertisement

Usage

This software is released into the public domain. You are free to use it in any way you like. You should save the Author text in the header files. If you modify it or extend it, please consider posting new code here for everyone to share. This software is provided “as is,” with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

My special thanks go to the other authors.

Downloads


Download source code – 3 Kb


Download demo project – 186 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.