Delete all columns in ListView
Trying to figure out how to delete all columns in a ListView. Currently, when I click on a button it populates a CListCtrl from data from a database.
I want to be able to clear all items and columns in the CListCtrl when I double click on a specific item in the List view Control and then re-populate it with new data.
It keeps adding columns everytime I double click.
Mike@spb
[409 byte] By [
mevasquez] at [2007-11-17 11:49:12]

# 1 Re: Delete all columns in ListView
Have you tried using CListCtrl::DeleteColumn()?
If you found this post to be helpful, please rate it to let others know it was useful.
# 2 Re: Delete all columns in ListView
Yes. This just deletes one column and if I have 6 columns then I have to call this 6 times. Is there anything like clearing all the columns or something like that. When I click on the first button, it create one column, I can delete this one with no problem, but when I double click on the CListCtrl, it will be creating about 20 columns. I want to be able to delete all those columns.
Mike@spb
# 3 Re: Delete all columns in ListView
Hmmm... don't know then.
Couldn't you just use DeleteColumn() in a loop?
int num_columns = 7; // or how many columns you have
for(int x=0;x<=num_columns;x++)
m_ListCtrl.DeleteColumn(x);
m_ListCtrl.Invalidate(true);
m_ListCtrl.UpdateWindow();
If you found this post to be helpful, please rate it to let others know it was useful.
# 4 Re: Delete all columns in ListView
Makethis as a member function
void DeleteAllColumns()
{
int ncols=pList->GetHeaderCtrl()->GetItemCount();
for(i=0;i<ncols;i++)
{
pList->DeleteColumn(i);
}
}
This will do the work for u
Badrinath
If u got helped Rate this..
# 5 Re: Delete all columns in ListView
In your code you have
int ncols=pList->GetHeaderCtrl()->GetItemCount();
I have tried this but does not work.
I'm not using a pointer to the listview. I do the following but it just gives me the item count but not the column count.
int ncols=m_List.GetItemCount();
What else do I need to get the column count so I can delete all the columns in a view list.
Mike@spb
# 6 Re: Delete all columns in ListView
I found a sample of your code but I'm getting an error:
error C2440: 'initializing' : cannot convert from 'int (__thiscall CHeaderCtrl::*)(void) const' to 'int'
# 7 Re: Delete all columns in ListView
Here's what I used to make it work:
void DeleteAllColumns
{
CListCtrl* pList= (CListCtrl *)GetDlgItem(IDC_LISTVIEW);
int nCol = pList->GetHeaderCtrl()->GetItemCount();
for (int i=0; i<nCol; i++)
{
pList.DeleteColumn(0);
}
}
# 8 Re: Delete all columns in ListView
The example show getting the item count from the header, not the listbox. Try this:
int ncols=m_List.GetHeaderCtrl()->GetItemCount();
You need to use your own variable names to make sample code work in your program.
# 9 Re: Delete all columns in ListView
To quick and easy delete every colum with minimal variables:
BOOL Deleted=TRUE;
while(Deleted==TRUE)
Deleted=DeleteColumn(0);
Simple and easy...
Large at 2007-11-10 8:14:59 >

# 10 Re: Delete all columns in ListView
Hi Large,
Your sample works beautifully. Thanks.
Also, I rated this post the best.