Link to home
Start Free TrialLog in
Avatar of cplau
cplau

asked on

Header Name of the listview?

Hi all,

How can I get name of the header at the listview?

Thanks!!!
ASKER CERTIFIED SOLUTION
Avatar of ShaunWilde
ShaunWilde

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cplau
cplau

ASKER

Hi,

I have already tried this but the program were forced to exit.

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

CHeaderCtrl* pHeader = (CHeaderCtrl*)wndList.GetDlgItem(0);
HD_ITEM headerinfo;
headerinfo.mask = HDI_TEXT;
      
if (!(pHeader->GetItem(0,&headerinfo)))
  AfxMessageBox("ERROR");

TRACE("HEADER NAME = %s\n", headerinfo.pszText);

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

What errors had I made?

Thanks for your opinion!!!
Retrieving the column caption


CString Caption; // will recieve the caption

// retrieve the header control see "The header control"
CHeaderCtrl* pHeader = (CHeaderCtrl*) m_listctrl.GetDlgItem(0);
int nMaxHeaderLength=100, // or whatever you need
    nColumnIndex;         // starts with zero, remember to initialize!

// fill the HDITEM structure
HDITEM HdItem;
HdItem.mask = HDI_TEXT; // just get the text
HdItem.pszText = Caption.GetBuffer(nMaxHeaderLength);
HdItem.cchTextMax = nMaxHeaderLength;

// retrieve the data
pHeader->GetItem(nColumnIndex, &HdItem);

// release the Buffer and finish
Caption.ReleaseBuffer();

TRACE("The caption of the Heading Number %i is :%s\n", nColumnIndex, Caption);

Retrieving the column caption


CString Caption; // will recieve the caption

// retrieve the header control see "The header control"
CHeaderCtrl* pHeader = (CHeaderCtrl*) m_listctrl.GetDlgItem(0);
int nMaxHeaderLength=100, // or whatever you need
    nColumnIndex;         // starts with zero, remember to initialize!

// fill the HDITEM structure
HDITEM HdItem;
HdItem.mask = HDI_TEXT; // just get the text
HdItem.pszText = Caption.GetBuffer(nMaxHeaderLength);
HdItem.cchTextMax = nMaxHeaderLength;

// retrieve the data
pHeader->GetItem(nColumnIndex, &HdItem);

// release the Buffer and finish
Caption.ReleaseBuffer();

TRACE("The caption of the Heading Number %i is :%s\n", nColumnIndex, Caption);

you need to fill in your HDITEM more as proskig has suggested
I don't think you have to get a pointer to the HeaderControl to get the header name. Instead use CListCtrl::GetColumn() to accomplish this task.

Here is the example:

CString strHeaderName;
int nHeaderLength = 100;

lvc.mask = LVCF_TEXT;
lvc.pszText = strName.GetBuffer(nHeaderLength);
lvc.cchTextMax = nHeaderLength;
m_listctrl.GetColumn(i, &lvc);

strName.ReleaseBuffer();
Correction:

CString strHeaderName;
int nHeaderLength = 100;

lvc.mask = LVCF_TEXT;
lvc.pszText = strName.GetBuffer(nHeaderLength);
lvc.cchTextMax = nHeaderLength;
m_listctrl.GetColumn(0, &lvc);

strName.ReleaseBuffer();

lvc.pszText will contain the name of the Header of column 1.