Tech Support > Microsoft Windows > Development Resources > List View - Insert Item
List View - Insert Item
Posted by magix on May 21st, 2008


Hi,

I have a list view control called IDC_LIST1 . I'm using win32, non-MFC.
I have an mdb access table and want to populate records into this list view.
The way the insertion into list view seems very confusing.


// some initialization / declaration of CDatabase, and CRecordSet here...
// some record set open, sql string..

while( !recset.IsEOF() )
{
// Copy each column into a variable
recset.GetFieldValue("Name",sz_Name);
recset.GetFieldValue("ID",sz_ID);
recset.GetFieldValue("Type",sz_Type);

// how to insert record into my list view IDC_LIST1, I read in googling
that there are item, subitem, etc.
// something like SendMessage to LVM_SETITEMSTATE / LVM_INSERTITEM

// can anyone give me a very simpler way to insert those into List View,
is it necessary to have subitem ?

// goto next record
recset.MoveNext();
}
// Close the database
database.Close();


Thanks.

Regards,
Magix


Posted by Kellie Fitton on May 21st, 2008


On May 21, 9:20*am, "magix" <ma...@asia.com> wrote:


Hi,

The following weblinks should give you some pointers:

http://msdn.microsoft.com/en-us/libr...35(VS.85).aspx

http://msdn.microsoft.com/en-us/libr...36(VS.85).aspx

Kellie.


Posted by r_z_aret@pen_fact.com on May 22nd, 2008


On Thu, 22 May 2008 00:20:59 +0800, "magix" <magix@asia.com> wrote:

Yes, it is a bit complex. First, you insert a row, then you add
columns to that row. Here are the functions I use. I use some private
functions that I did not include; you can either ignore them or figure
out their function easily enough.



// ----------------------------------------------------------
// InsertLine
BOOL ClsPFListView::InsertLine( int nIndex, LPCTSTR sStrings[], int
nColumns )
{
TEST_VALID( GetCountColumn() == nColumns );

int iCol = 0;
int jIndex = InsertString( nIndex, sStrings[iCol] );
#if ISDEBUG
const int iMax = 80;
ASSERT( nIndex < GetCount() );
if (nIndex >= 0)
ASSERT( jIndex == nIndex );
TCHAR sTest[iMax + 1];
int nFound = GetString( jIndex, sTest, iMax );
ASSERT( nFound >= 0);
ASSERT( nFound < iMax );
ASSERT( PFSSamePiece( sStrings[iCol], sTest ) );
#endif

LPTSTR s = NULL;
for ( iCol = 1; iCol < nColumns; iCol++)
{
ASSERT( ColumnExists( iCol ) );
if (PFSArgIsEmpty( sStrings[iCol] ))
s = _T( "" ); //lint !e605 non-const pointer used for
const
else
s = const_cast<LPTSTR>(sStrings[iCol]);
// Less feedback, less overhead
ColumnSetString( jIndex, iCol, s );
#if ISDEBUG
nFound = ColumnGetString( jIndex, iCol, sTest, iMax );
ASSERT( nFound >= 0);
ASSERT( nFound < iMax );
ASSERT( PFSSameString( sStrings[iCol], sTest ) );
#endif
}

ASSERT( iCol == nColumns );

return TRUE;

} // InsertLine


// ----------------------------------------------------------
// InsertString
int ClsPFListView::InsertString( int nIndex, LPCTSTR sString )
{
ASSERT( IsWindow() ); // 11 Dec 04 (7.7.0.11)
ASSERT( !PFSArgIsEmpty( sString ) ); // 10 Mar 06 (8.0.0.3)
LV_ITEM vItem;
adgINITSTRUCT_NoSize( vItem ); // sets size member of vItem

if (PFSArgIsEmpty( sString ))
sString = _T( "" );

if (nIndex < 0 || nIndex > GetCount())
nIndex = GetCount();

if (m_nMax > 0 && GetCount() >= m_nMax)
{
if (!m_bShowedMaxWarning)
{
m_bShowedMaxWarning = TRUE;
TCHAR sMsg[MAX_PATH + 50];
_stprintf( sMsg, _T( "Only first %d will be shown" ),
m_nMax );
ASSERT( GetMessageP() != NULL );
ASSERT( GetGenInfoP() != NULL );
IGNORE_RETURN( ClsPFMessage::MBoxS(
sMsg,
ClsPFMessage::Warn,
ClsPFRoot::GetGenInfoP()->GetInfo(
ClsPFGeneralInfo::PRETTYNAME )) );
}
return -1;
}

// vItem.mask = LVIF_TEXT | LVIF_DI_SETITEM;
// vItem.mask = LVIF_TEXT | LVIF_PARAM | LVIF_DI_SETITEM |
LVIF_NORECOMPUTE;
vItem.mask = LVIF_TEXT | LVIF_PARAM; // See related note in
SetItem
vItem.iItem = nIndex;
vItem.pszText = const_cast<LPTSTR>(sString);
vItem.cchTextMax = static_cast<int>(_tcslen( sString )); // See
related note in SetItem
vItem.lParam = static_cast<LPARAM>(nIndex);
#if ISDEBUG
int j = ListView_InsertItem( m_hWnd, &vItem );
ASSERT( j == nIndex );
#else
// 22 Apr 06 (8.0.0.6)
IGNORE_RETURN( ListView_InsertItem( m_hWnd, &vItem ) );
#endif

m_nCount++;

ASSERT( nIndex < GetCount() );
ASSERT( GetCount() == m_nCount );

VERIFY( SetHExtent( sString ) );

return nIndex;

} // InsertString


// ----------------------------------------------------------
// ColumnSet
BOOL ClsPFListView::ColumnSet( int iCol, LPCTSTR sLabel, int nWidth,
LVColumnAlign cAlign )
{
// TODO: add support for auto-sizing (LVSCW_AUTOSIZE_USEHEADER)
see 15 Sep 03
// contribution from William Mahoney to thread called "Resizing
columns in CHeaderCtrl"
// in comp.os.ms-windows.programmer.win32
ASSERT( IsWindow() ); // 11 Dec 04 (7.7.0.11)
ASSERT( !PFSArgIsEmpty( sLabel ) ); // 10 Mar 06 (8.0.0.3)
ASSERT( iCol >= 0 );
ASSERT( iCol < m_nColumn );
ASSERT( m_abSortDir.GetCount() == m_nColumn );

LV_COLUMN vCol;

vCol.mask = LVCF_SUBITEM;
vCol.iSubItem = iCol;

// Can't change alignment for column 0
if (cAlign != LVCAny && iCol != 0)
{
vCol.mask |= LVCF_FMT;
if (cAlign != LVCAny)
vCol.fmt = static_cast<int>(cAlign);
}

if (PFSArgIsEmpty( sLabel ))
sLabel = _T( "" );
vCol.mask |= LVCF_TEXT; // See
related note in SetItem
#if !defined( SORT_INDICATOR_ONE )
ClsPFString pfTemp;
if (m_nSortMarkColumn >= 0)
pfTemp.Formate( _T( "%s%s" ), m_pfsDescendMark.ToT(), sLabel
);
else
pfTemp = sLabel;
vCol.pszText = const_cast<LPTSTR>(pfTemp.ToT());
#error Line to set cchTextMax below should use length of pfTemp
#else
vCol.pszText = const_cast<LPTSTR>(sLabel);
#endif
vCol.cchTextMax = static_cast<int>(_tcslen( sLabel )); // See
related note in SetItem

if (nWidth > 0)
{
vCol.mask |= LVCF_WIDTH;
vCol.cx = nWidth;
}

SetAutoHScroll( FALSE ); // Sets m_bAutoHScroll

return ListView_SetColumn( m_hWnd, iCol, &vCol );

} // ColumnSet



// ----------------------------------------------------------
// SetHExtent
BOOL ClsPFListView::SetHExtent( LPCTSTR sString )
{
ASSERT( IsWindow() ); // 11 Dec 04 (7.7.0.11)

if (m_bAutoHScroll && m_pfsLongest.ULen() < PFSULen( sString ))
{
m_pfsLongest = sString;
// Copied from ClsPFListBox::SetHExtent
SIZE sz;
HDC dc = ::GetDC( m_hWnd );
int nSave = ::SaveDC( dc );
HFONT hF = GetWindowFont( m_hWnd ), // GetWindowFont is a
macro
hFO = SelectFont( dc, hF ); // SelectFont is a
macro
DONT_ASSERT( hF != NULL );
DONT_ASSERT( hFO == NULL );

VERIFY_EXPLAIN( ::GetTextExtentPoint32( dc, m_pfsLongest,
m_pfsLongest.NLen(), &sz ) );
int nSize = sz.cx;

if (hFO != NULL)
VERIFY( SelectFont( dc, hFO ) == hF );
#if 0
// CleanupObject fails. Perhaps because font is still selected
in real DC
CleanupObject( hF );
#endif

::RestoreDC( dc, nSave );
VERIFY( ::ReleaseDC( m_hWnd, dc ) );
dc = NULL; // 11 Jun 05 (7.7.0.35)
// NOTE: For future uses of DC, delete tools (pen, brush,
etc.) _after
// restoring and releasing DC (Richter & Locke p. 72).
#if 0
// Disabled some time before 10 Aug 07 (8.1.0.13)
nSize += ::GetSystemMetrics( SM_CXVSCROLL );
#endif
return ColumnSetWidth( 0, nSize ); // Calls
ListView_ColumnSetWidth
}

return TRUE;

} // SetHExtent



-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
Useful reading (be sure to read its disclaimer first):
http://catb.org/~esr/faqs/smart-questions.html

Posted by magix on May 22nd, 2008


Thanks for input.
Issue resolved. Please ignore.

<r_z_aret@pen_fact.com> wrote in message
news:c7oa34lu126mj9nlbei17ljeh4ceqfn27t@4ax.com...



Similar Posts