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);
}