Adding a column


The CListCtrl::InsertColumn() is used to add a column to the list view control. This function can be used even if the control is not in the report mode. When using this function you should remember to specify the nSubItem argument (or the iSubItem field if you are using the LV_COLUMN structure). The nSubItem is usually equal to the column number. Here is a helper function that makes adding a column easier.

// AddColumn    - adds column after the right most column
// Returns      - the index of the new column if successful or -1 otherwise.
// sColHeading  - the column's heading
// nWidth       - Width of the column, in pixels. If this parameter is -1, 
//                the column width is the same as previous column
// nFormat      - the alignment of the column. Can be LVCFMT_LEFT,
//                LVCFMT_RIGHT, or LVCFMT_CENTER.
int CMyListCtrl::AddColumn(LPCTSTR sColHeading, int nWidth /* = -1*/, int nFormat /* = LVCFMT_LEFT*/)
{
        CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
        int nColumnCount = pHeader->GetItemCount();

        if( nWidth == -1 )
        {
                // The line below return zero for ICON views
                //nWidth = GetColumnWidth( nColumnCount - 1 );

                // Get the column width of the previous column from header control
                HD_ITEM hd_item;
                hd_item.mask = HDI_WIDTH;               //indicate that we want the width
                pHeader->GetItem( nColumnCount - 1, &hd_item );
                nWidth = hd_item.cxy;
        }
        return InsertColumn( nColumnCount, sColHeading, nFormat, nWidth, nColumnCount );
}


Comments

  • Adding A Column Heading

    Posted by Legacy on 11/02/2003 12:00am

    Originally posted by: Terri

    What is the function InsertColumn that is used in the return at the end of the code segment?

    Reply
  • Add problems with CListview

    Posted by Legacy on 05/09/2000 12:00am

    Originally posted by: Chris Klecker

    There appears to be a hick-up when changing the number of columns in a CListview window back and forth. When it starts out, at 3 columns it seems to work and look fine. When changing the number of columns to 7 there seems to be an eighth column that is increasing in size. When I revert back to the 3 column view this eighth column, which is now the fourth column, is infinitely large. This also seems to be causing some problems in the control as well as I cannot click on any items in the view and the first column appears to not be painting properly.

    Has anyone else seen this and has there been any documented help in getting around this problem?

    Thanks!

    Chris Klecker

    NOTE:

    I've managed to find the error of my ways. In using the following ListCtrl.DeleteColumn instead of ListCtrl.GetHeaderCtrl()->DeleteItem() it clears the problem.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds