How to insert values to Listview…


How to insert values to a listview in report format with check boxesHere view try to insert 5 values to the list view when we press on the OK Button.

First you create a dialogue based MFC project .then Copy the below codes into the eventĀ  OnInit() or OnPaint()

In the below code

  • IDC_LIST1 is ID of your listview placed in the dialog
  • m_Clist1 is the ctrl veriable of your listview.

//Code to place in the OnPaint()/OnInint()

{

ListView_SetExtendedListViewStyle(::GetDlgItem(m_hWnd,IDC_LIST1),LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES|LVS_SORTASCENDING);

// Header Placement
CWnd* pWnd = GetDlgItem( IDC_LIST1 );
CRect rect;
pWnd->GetClientRect(&rect);
int nColInterval = rect.Width()/100;

m_Clist1.InsertColumn(0, L”SerialNO”, LVCFMT_LEFT, nColInterval * 20);
m_Clist1.InsertColumn(1, L”Header One”, LVCFMT_LEFT, nColInterval * 24);
m_Clist1.InsertColumn(2, L”Header Two”, LVCFMT_LEFT, nColInterval * 40);
m_Clist1.InsertColumn(3, L”Header Three”, LVCFMT_LEFT, nColInterval * 25);

}

// Code to place in the OK button

{

m_Clist1.DeleteAllItems();//For clearing the items

for(int i = 0; i < 5; i++)
{

LVITEM lvItem;
lvItem.mask = LVIF_TEXT;
lvItem.iItem = i;
lvItem.iSubItem = 0;
CString ListCount;
ListCount.Format(_T(“%d”), i + 1 );
lvItem.pszText = (LPTSTR)(LPCTSTR)(ListCount);
int nItem = m_Clist1.InsertItem(&lvItem);
m_Clist1.SetItemText(nItem, 1,L”Data 1″);
m_Clist1.SetItemText(nItem, 2,L”Data 2″);
m_Clist1.SetItemText(nItem, 3,L”Data 3″);

}

}

Leave a comment