CListCtrl数据显示问题

本文描述了一个CListCtrl在MFC中遇到的问题,即在第二次显示数据时,控件无法正常更新数据,尽管数据已写入但无法呈现。问题源于在显示新数据前清空标题条目,而注释掉清空标题栏的代码后,数据恢复正常显示。解决方案是首次设置标题后,避免动态增删,仅修改标题内容。
摘要由CSDN通过智能技术生成

问题描述:将CListCtrl无法正常显示数据,第一次显示数据时完全正常,第二次显示数据时,控件不显示数据,利用强制刷新也不行。经调试发现,数据已经写入CListCtrl控件内存,只是因为某种原因无法呈现出来。

原因:第一次显示数据之后,首先清空了控件中的所有记录,然后清空了所有的标题条目;在下一次数据显示之前,重新初始化标题栏,添加记录。清空标题栏的代码注释掉之后,数据就能正常显示了。

解决方案:在第一次为控件增加标题之后,不再动态增删标题,对标题修改显示即可。具体代码如下:


void CCXXXView::OnInitialUpdate(){
	CListView::OnInitialUpdate();

	//设置样式
	CListCtrl &List = GetListCtrl();

#ifdef _DEBUG
	long lStyle = GetWindowLong(List.GetSafeHwnd(), GWL_STYLE);
	//long lStyle = GetWindowLong(List.m_hWnd, GWL_STYLE);
	lStyle&=~LVS_TYPEMASK;
	lStyle|= (LVS_REPORT);
	//lStyle |= (LVS_REPORT|LVS_SHOWSELALWAYS);
	SetWindowLong(List.m_hWnd, GWL_STYLE, lStyle);
	DWORD dwExStyle = List.GetExtendedStyle();
	//dwExStyle |= (LVS_EX_FULLROWSELECT  | LVS_EX_GRIDLINES);
	dwExStyle |= (LVS_EX_FULLROWSELECT  );
	List.SetExtendedStyle(dwExStyle);
#else
	//DWORD dwStyle = GetWindowLong(List.GetSafeHwnd(), GWL_STYLE);
	long lStyle = GetWindowLong(List.GetSafeHwnd(), GWL_STYLE);
	//dwStyle |= (LVS_REPORT);
	lStyle |= (LVS_REPORT|LVS_SHOWSELALWAYS);
	SetWindowLong(List.GetSafeHwnd(), GWL_STYLE, lStyle);
	DWORD dwExStyle = List.GetExtendedStyle();
	dwExStyle |= (LVS_EX_FULLROWSELECT );
	List.SetExtendedStyle(dwExStyle);

#endif
	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
}
void CCXXXView::ResetListCtrl( int nselItem )
{
	ClearListHeader();
	
	InitListHeader( nselItem );

}

void CCXXXView::ClearListHeader()
{
	CListCtrl &List = GetListCtrl();
	List.DeleteAllItems();
	//CHeaderCtrl *clch = List.GetHeaderCtrl();
	//if( clch != NULL )
	//{
	//	int nItem = clch->GetItemCount();
	//	for( int i = nItem - 1; i >= 0; i-- )
	//	{
	//		clch->DeleteItem( i );
	//		
	//	}
	//}


}

void CCXXXView::InitListHeader( int nselItem )
{
	CListCtrl &List = GetListCtrl();
	CHeaderCtrl *chcl = List.GetHeaderCtrl();
	int nColCount = chcl->GetItemCount();
	if( nColCount <= 0 )
	{
		//strHead.clear();
		//strHead = g_ChiticWksLng.GetString(szSecContentList, "root_head_index", "索引");
		List.InsertCol
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中使用链表和CListCtrl来实现数据的页显示,你可以将链表中的数据逐个添加到CListCtrl控件中,并根据当前页码和分页大小显示对应的数据。 以下是一个示例代码,展示了如何使用链表和CListCtrl来实现数据的分页显示: ```cpp #include <afxwin.h> #include <afxcmn.h> #include <vector> struct DataItem { int id; CString name; }; class CMyDialog : public CDialog { private: CListCtrl m_listCtrl; std::vector<DataItem> m_data; int m_pageSize; int m_currentPage; public: CMyDialog() : CDialog(IDD_MYDIALOG) { m_pageSize = 5; m_currentPage = 1; } protected: virtual BOOL OnInitDialog() { CDialog::OnInitDialog(); // 创建CListCtrl控件 m_listCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT, CRect(10, 10, 300, 200), this, IDC_LIST_CTRL); m_listCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT); // 添加列标题 m_listCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 50); m_listCtrl.InsertColumn(1, _T("Name"), LVCFMT_LEFT, 100); // 添加示例数据到链表 for (int i = 1; i <= 20; i++) { CString name; name.Format(_T("Item %d"), i); m_data.push_back({i, name}); } // 显示第一页数据 ShowPage(m_currentPage); return TRUE; } void ShowPage(int pageNumber) { // 清空列表 m_listCtrl.DeleteAllItems(); // 计算当前页的起始索引和结束索引 int startIndex = (pageNumber - 1) * m_pageSize; int endIndex = startIndex + m_pageSize - 1; if (endIndex >= m_data.size()) { endIndex = m_data.size() - 1; } // 添加当前页的数据到列表 for (int i = startIndex; i <= endIndex; i++) { const DataItem& item = m_data[i]; int index = m_listCtrl.InsertItem(i - startIndex, _T("")); m_listCtrl.SetItemText(index, 0, CString(item.id)); m_listCtrl.SetItemText(index, 1, item.name); } } afx_msg void OnBnClickedPrevButton() { if (m_currentPage > 1) { m_currentPage--; ShowPage(m_currentPage); } } afx_msg void OnBnClickedNextButton() { int totalPages = (m_data.size() + m_pageSize - 1) / m_pageSize; if (m_currentPage < totalPages) { m_currentPage++; ShowPage(m_currentPage); } } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(CMyDialog, CDialog) ON_BN_CLICKED(IDC_PREV_BUTTON, &CMyDialog::OnBnClickedPrevButton) ON_BN_CLICKED(IDC_NEXT_BUTTON, &CMyDialog::OnBnClickedNextButton) END_MESSAGE_MAP() int main() { AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); CMyDialog dlg; dlg.DoModal(); return 0; } ``` 在这个示例代码中,创建了一个自定义的对话框类`CMyDialog`,其中包含一个CListCtrl控件用于显示数据。 在`OnInitDialog`函数中,首先创建了CListCtrl控件,并设置了扩展样式和列标题。然后,添加了一些示例数据到链表。 `ShowPage`函数用于根据当前页码显示对应的数据。首先,清空列表中的所有项。然后,根据当前页码计算起始索引和结束索引,并将对应的数据逐个添加到列表中。 在`OnBnClickedPrevButton`和`OnBnClickedNextButton`消息处理函数中,分别处理上一页和下一页按钮的点击事件。通过更新当前页码并调用`ShowPage`函数来显示上一页或下一页的数据。 最后,在`main`函数中创建了`CMyDialog`对象,并显示对话框。 运行这段代码,你会看到一个带有上一页和下一页按钮的对话框,点击按钮可以切换数据的分页显示。 这个示例代码基于MFC框架,使用了MFC的消息映射和对话框类。如果你不熟悉MFC,你可以根据自己的需求将相关代码移植到其他框架或平台上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值