Using virtual CListView in a dynamic set

Source: Internet
Author: User
Tags bool count new features access database

This example illustrates how to use an Access database file with the IE4 virtual list view.

Loading large amounts of data into CListView derived classes is a very slow process, even when data is in memory. It is much slower to access data in the database. A list of new features is the ability to reload when it needs data. Here is the concrete step.

You must install IE3 later version of the COMCTL32.DLL.

Open your database file.

Use your document object to open your database files and recordset. In this example, I used a dynaset-type Recordset. This allows me to use the CDaoRecordset SetAbsolutePosition member function to set the current record relative record number of a Recordset object.

BOOL CVirtualListDoc::OnOpenDocumentFile(LPCTSTR lpszPathname)
{
CString m_FileName = lpszPathname;
pDBase = new CDaoDatabase;
pDBase->Open(m_FileName);
CString strSQL = "SELECT * FROM TableName ORDER BY ColumnName";//Set up SQL statement
pRecordSet = new CDaoRecordset(pDBase);
pRecordSet->Open(dbOpenDynaset, strSQL);//Open recordset using SQL statement
pRecordSet->MoveLast();//you have to access the records in the dynaset to get GCDaoRecordSet::etRecordCount() to work
}

Set this list to view the style as Lvs_ownerdata;

BOOL CVirtualListView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszName = WC_LISTVIEW;
cs.style &= ~LVS_TYPEMASK;
cs.style |= LVS_REPORT;
cs.style |= LVS_EDITLABELS;
cs.style |= LVS_OWNERDATA;
CListView::PreCreateWindow(cs);
}

Sets the number of items in the list. You need to tell the list how many items will be included. This is controlled by sending a lvn_setitemcount to the list. Do this in the OnInitialUpdate () member function of the list, and set the number of columns that the list sees. You can send a Lvm_setextendlistviewstyle message to the control to set some new extensions to the list view style.

void Cvirtuallistview::oninitialupdate ()
{
clistview::oninitialupdate ();
/*set number of items in List to number of items in recordset*/
/* Create image list*/
Imagelist.create (idb_imagelist, 1, RGB (0,0,0)); br> GetListCtrl (). SetImageList (&imagelist, Lvsil_small);
/* Set extended stlyes*/
DWORD dwExStyle = Lvs_ex_fullrowselect | Lvs_ex_gridlines | /*lvs_ex_subitemimages |*/
Lvs_ex_headerdragdrop | Lvs_ex_trackselect;
GetListCtrl (). SendMessage (lvm_setextendedlistviewstyle, 0, LPARAM (dwexstyle));
Lv_column Lvcolumn;
Lvcolumn.mask = lvcf_fmt | Lvcf_width | Lvcf_text | Lvcf_subitem;
Lvcolumn.fmt = Lvcfmt_left;
Lvcolumn.cx = 120;
for (int i = 0; i < GetFieldCount (); i++)//Set up columns
{
CDaoFieldInfo fieldinfo;
Precordset->g Etfieldinfo (i, FieldInfo);//get field name
int len = Fieldinfo.m_strName.GetLength ();
CString temp = fieldinfo.m_strname;
tchar* szbuffer = new Tchar[len + 1];
Strcpy (Szbuffer, temp. GetBuffer (len));
Temp. ReleaseBuffer ();
Lvcolumn.psztext = szbuffer;
GetListCtrl (). InsertColumn (i, &lvcolumn);//insert column
Delete szbuffer;
}
/*set number of items in listview*/
Count = Precordset->getrecordcount ();//get number of records
GETLISTCT RL (). SendMessage (Lvm_setitemcount, (WPARAM) count, (LPARAM) lvsicf_noinvalidateall);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.