Click here to Skip to main content
15,888,527 members
Articles / Desktop Programming / MFC

SuperGrid - Yet Another listview control

Rate me:
Please Sign up or sign in to vote.
4.77/5 (98 votes)
8 Dec 1999CPOL3 min read 1M   11.4K   345   192
A combination list control and tree control with checkbox capability

Sample Image

Okay yet another full blown ownerdraw listview control with a tree like thing in the first column.

What´s new:

  • subitems now supports images
  • improved sorting
  • bugfixes reported by you.

please see the history section in the SuperGridCtrl.cpp file for further reading.

Features:

The listview control has support for editing subitems and basic user navigation. To edit an item hit the Enter key to start editing, when done editing, hit the Enter Key again. The listview control also has support for sorting items, drag and drop, basic scroll, autoscroll and autoexpand when dragging an item over another item. The listview control supports Insert/Delete items and you can use the +,-,* keys to expand/collapse items. The listview control does NOT support header drag/drop operations. Thanks for all them positive emails I have recived. If you have any suggestions to some future version let me know.

Note:

The class CSuperGridCtrl is derived from CListCtrl and it has a nested class called CTreeItem. The CTreeItem represents a node in a linked list. Each CTreeItem has a CObList representing its children and a few data members telling you the current status on the node e.g expanded, haschildren etc. Each listview item has a pointer to the CTreeItem(stored in the lParam of course). The CTreeItem has an associated class called CItemInfo. The CItemInfo class represents the data in the listview. This class is just a wrapper for the CStringArray, each item in the array represents a subitem. You may have to modify this class to suit your logical data-representation in the listview. In each CItemInfo you may associate a controltype: default controltype is an edit-control. In the source code you will find an example on how to associate a combobox control to a specific CItemInfo and how to initalize the combobox with default values.

Another standard control which is implicit in the listview control are the checkbox-control, by using the LVS_EX_CHECKBOXES style you will have this control for free, nothing new here....but in the CItemInfo class you will be able to test which items are checked. The class CMySuperGrid which is derived from CSuperGridCtrl shows you how to initalize the grid, insert root items, insert items, sort items, associate controltypes and how to implement 'print preview' selected or checked items depending on which extended style you have set, it also shows you have to search for items, select items, delete items and how to set the current listview icon and individual cell color. The source code shows you how to use the listview control in a CView derived class and in a CDialog derived class.

The listview control was built with Visual C++ 6.0 (sp10) level 8 on Windows 2000 beta 9, I don't remember the Netcard ID ;-).

In the SuperGridCtrl.h header file you will find further information/documentation on how to use this listview control. I have included a "history" section in the SuperGridCtrl.cpp file.

Drop me a line when you find that bug or if you have any comments / suggestions / improvement at all to this listview control.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to get rid of the grid line? Pin
15-May-02 7:32
suss15-May-02 7:32 
AnswerRe: how to get rid of the grid line? Pin
4-Jun-02 10:38
suss4-Jun-02 10:38 
QuestionIn TabCtrl problem? Pin
haoshi14-May-02 22:05
haoshi14-May-02 22:05 
Generalcheckbox lost in dialog Pin
23-Apr-02 18:45
suss23-Apr-02 18:45 
GeneralRe: checkbox lost in dialog Pin
21-May-02 2:52
suss21-May-02 2:52 
Generalnewer code at codetools Pin
mloibl10-Apr-02 22:50
mloibl10-Apr-02 22:50 
GeneralDrag n Drop Pin
15-Feb-02 2:41
suss15-Feb-02 2:41 
GeneralRe: Drag n Drop Pin
akraus19-Mar-02 23:21
akraus19-Mar-02 23:21 
I did use the Microsoft Drag and Drop classes. It works fine.
Below is an example of my Symbol Grid which is derived from CSuperGridCtrl.

You need do register the clippboard format You want to usein the ctor of CSymbolGrid:

CSymbolGrid::CSymbolGrid()
{
DragDropFormat = ::RegisterClipboardFormat(GLOBAL_CLIPBOARD_FORMAT);
}

void CSymbolGrid::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{

CString lAddress,lLength;
int SelItem = GetSelectedItem();

GetSelAddress(lAddress);
GetSelLength(lLength);

// Create the drag&drop source and data objects
COleDropSource *pDropSource = new COleDropSource;
COleDataSource *pDataSource = new COleDataSource;

// now grab the data (here: the count and text)
// and serialize it into an clipboard archive

HGLOBAL hGlob;


try // to drag into a memory dump
{
CSharedFile file(GMEM_ZEROINIT|GMEM_DDESHARE|GMEM_MOVEABLE);

CArchive ar(&file, CArchive::store);
// "Serialize" your data object into the archive
// (yes, you may use YourObject.Serialize(ar) here!)
CPtrArray lptrArr;
OnAddVar(lptrArr);
WORD larrSize = lptrArr.GetSize();

ar << lAddress << lLength << larrSize;

ar.Close();

// put the file object into the data object
hGlob = file.Detach();
pDataSource->CacheGlobalData(DragDropFormat, hGlob);
pDataSource->DoDragDrop(DROPEFFECT_MOVE|DROPEFFECT_COPY, NULL, pDropSource);
}
catch(...)
{
// exception while destructing the file
ASSERT(FALSE);
}

delete pDropSource;
delete pDataSource;

*pResult = 0;
}

In the Target Window You must also Register the ClipBoard Format.
For the Drop Target I use a derived class

class CViewDropTarget : public COleDropTarget
{
public:
CViewDropTarget();
virtual ~CViewDropTarget();
void SetClassPtr(CView *lptr) { ViewPtr = lptr; };
private:

CView *ViewPtr;
UINT DragDropFormat;

public:
CString Address; // Address we got by Drop operation
CString Length; // Length we got by Drop operation

protected:
virtual BOOL OnDrop( CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point );
virtual DROPEFFECT OnDragOver( CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point );
};


CViewDropTarget::CViewDropTarget()
{
DragDropFormat = ::RegisterClipboardFormat(GLOBAL_CLIPBOARD_FORMAT);
ViewPtr=NULL;
}

CViewDropTarget::~CViewDropTarget()
{

}

DROPEFFECT CViewDropTarget::OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point )
{
CFile *pFile = pDataObject->GetFileData(DragDropFormat);
if (pFile != NULL)
{
// perhaps some point checking here?
delete pFile; // Is a must, because every time a Shared memory object is created,
// which must be deleted !

return DROPEFFECT_COPY; // data fits
}
else
return DROPEFFECT_NONE; // data won&#8217;t fit
}

BOOL CViewDropTarget::OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point )
{

CFile *pFile = pDataObject->GetFileData(DragDropFormat);
if (pFile != NULL)
{
// perhaps some point checking first?
try
{
CArchive ar(pFile, CArchive::load);

if (DragDropFormat != CF_TEXT)
{
// "Serialize" your data object from the archive
// (yes, you may use YourObject.Serialize(ar) here!)
ar >> Address >> Length;
}

ar.Close();

}
catch(...)
{
// exception in the destructor of ar
ASSERT(FALSE);
}
}

delete pFile; // Is a must, because every time a Shared memory object is created,
// which must be deleted !

// notify our view from a drop operation
SendMessage(ViewPtr->m_hWnd,WM_USER_DROP_MEM_DUMP,0,0);

return COleDropTarget::OnDrop(pWnd, pDataObject, dropEffect, point);
}

Create an Instance of this class in Your View

class CMyView : CView
{
private:
CViewDropTarget DropTarget
}

CMyView::CMyView()
{
DropTarget.SetClassPtr(this); // mark it as target for our sendmessage function
}
in the View ctor set the view ptr to make it for the class possible to send a message when
a drop operation has occured.

CMyView::OnInitialUpdate()
{
// register Your view as drop target
if( !DropTarget.Register(this) )
ASSERT(FALSE);
}


Thats all I think. It works great.




QuestionHelp!! How do I edit all the items in listview(ie. not just the first column)? Pin
12-Feb-02 16:30
suss12-Feb-02 16:30 
AnswerRe: Help!! How do I edit all the items in listview(ie. not just the first column)? Pin
14-Mar-02 2:50
suss14-Mar-02 2:50 
Generalthanks Pin
Mohamed KHADRAOUI6-Feb-02 3:16
Mohamed KHADRAOUI6-Feb-02 3:16 
QuestionHow to display an icon( ! ) in the center of the column Pin
21-Jan-02 2:04
suss21-Jan-02 2:04 
GeneralSort Pin
4-Jan-02 9:58
suss4-Jan-02 9:58 
QuestionHow can I make Multiline.... Pin
chunghansu1-Jun-01 20:58
chunghansu1-Jun-01 20:58 
QuestionPorting to the web?!? Pin
David DeBoer7-May-01 8:51
David DeBoer7-May-01 8:51 
QuestionHow to update up to date Pin
6-May-01 16:41
suss6-May-01 16:41 
GeneralSuperGrid in VisualFox Pin
Stefan Durac26-Apr-01 6:14
Stefan Durac26-Apr-01 6:14 
GeneralRe: SuperGrid in VisualFox Pin
26-Apr-01 22:20
suss26-Apr-01 22:20 
GeneralUsing it in a Dialog Pin
Edilson Vasconcelos de Melo Junior24-Apr-01 8:55
Edilson Vasconcelos de Melo Junior24-Apr-01 8:55 
GeneralRe: Using it in a Dialog Pin
26-Apr-01 22:16
suss26-Apr-01 22:16 
GeneralRe: Using it in a Dialog Pin
Orlando Cardoso15-Jun-01 5:29
Orlando Cardoso15-Jun-01 5:29 
Generalnew email Pin
3-Apr-01 20:17
suss3-Apr-01 20:17 
GeneralRe: new email Pin
17-Apr-01 5:23
suss17-Apr-01 5:23 
QuestionWhat a pity... Pin
Jason Troitsky26-Feb-01 4:25
Jason Troitsky26-Feb-01 4:25 
AnswerRe: What a pity... Pin
3-Aug-01 17:11
suss3-Aug-01 17:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.