这个问题似乎不难啊。可是我不会。帮帮我吧大家。
具体情况是:1,我是用CListView作为数据显示和操作的界面。2,我在CListView中加了一个成员函数ShowAll()来填充&listCtrl数据。ShowAll()中,我打开数据库,并把我需要的数据填入到&listctrl中。3,我发现每次填充数据时都只能从列表控件的第一(0)行插入
麻烦大家帮我想象办法吧。谢谢了。
具体情况是:1,我是用CListView作为数据显示和操作的界面。2,我在CListView中加了一个成员函数ShowAll()来填充&listCtrl数据。ShowAll()中,我打开数据库,并把我需要的数据填入到&listctrl中。3,我发现每次填充数据时都只能从列表控件的第一(0)行插入
麻烦大家帮我想象办法吧。谢谢了。
再InsertItem()
再SetItemText()list1->InsertColumn(0,"name",LVCFMT_LEFT,100,-1);
list1->InsertItem(i,"");
list1->SetItemText(i,0,"aaa");如果不对.请多包函.
{
//////////////////////////////////////////////////////////////////////////
// Open recordset
int iNum = 0;
CListCtrl &List = GetListCtrl();
List.DeleteAllItems();
CMainFrame *pMainFrm = (CMainFrame*) AfxGetMainWnd();
CString strSQL = "select * from dms";
if (!OpenRecordSet(m_pRecordset, strSQL)) {
AfxMessageBox("无法打开数据库");
return;
}
m_pRecordset->MoveFirst();
while (!m_pRecordset->adoEOF) {
//////////////////////////////////////////////////////////////////////////
// prepare to input value to cellsCString strNumber, strName, strIDNumber, strGender, strAddress, strAge, strModifyTime;
COleDateTime oleBirthdate, oleModifyTime;
COleDateTime oleCurrentTime = COleDateTime::GetCurrentTime();
strName = pMainFrm->VariantToCString(m_pRecordset->GetCollect("姓名"));
strIDNumber = pMainFrm->VariantToCString(m_pRecordset->GetCollect("居民证号码"));
strAddress = pMainFrm->VariantToCString(m_pRecordset->GetCollect("常住地址"));
strGender = pMainFrm->VariantToCString(m_pRecordset->GetCollect("性别"));
oleBirthdate = m_pRecordset->GetCollect("出生日期");
oleModifyTime = m_pRecordset->GetCollect("记录修改时间");
iNum++;
strNumber.Format("%d", iNum);
int iAge = oleCurrentTime.GetYear() - oleBirthdate.GetYear();
strAge.Format("%d", iAge);
strModifyTime.Format("%d年%d月%d日", oleModifyTime.GetYear(), oleModifyTime.GetMonth(), oleModifyTime.GetDay());
// start!
List.InsertItem(0, strNumber); //序号
List.SetItemText(0, 1, strName); //姓名
List.SetItemText(0, 2, strAge); //年龄
List.SetItemText(0, 3, strGender); //性别
List.SetItemText(0, 4, strIDNumber); // 身份证号码
List.SetItemText(0, 5, strAddress); //地址
List.SetItemText(0, 6, strModifyTime); //最后修改时间
m_pRecordset->MoveNext();
}
}
我调试运行过,发现是了一个access violate错误,可能是视类初始化时不能使用MainFrame对象吧,我把要用到的转化函数做成视类的成员函数试试看。真是小问题引起了大麻烦。也请大家继续帮忙我分析一下,谢谢。
this->GetListCtrl().InsertColumn(1,"Size", LVCFMT_LEFT, 100);
this->GetListCtrl().InsertColumn(2,"Percent", LVCFMT_LEFT, 100); int nIndex = this->GetListCtrl().InsertItem(0,"1");
this->GetListCtrl().SetItemText(nIndex, 1, "11");
this->GetListCtrl().SetItemText(nIndex, 2, "111"); nIndex = this->GetListCtrl().InsertItem(1,"2");
this->GetListCtrl().SetItemText(nIndex, 1, "22");
this->GetListCtrl().SetItemText(nIndex, 2, "222"); nIndex = this->GetListCtrl().InsertItem(2,"3");
this->GetListCtrl().SetItemText(nIndex, 1, "33");
this->GetListCtrl().SetItemText(nIndex, 2, "333");
ShowAll() 开始代码处设置个断点 F5这个就是为什么只能从0行插入的原因
List.InsertItem(0, strNumber); //序号
List.SetItemText(0, 1, strName); //姓名
List.SetItemText(0, 2, strAge); //年龄
List.SetItemText(0, 3, strGender); //性别
List.SetItemText(0, 4, strIDNumber); // 身份证号码
List.SetItemText(0, 5, strAddress); //地址
List.SetItemText(0, 6, strModifyTime); //最后修改时间 修改为
int nItem = List.GetItemCount();
List.InsertItem(nItem, strNumber); //序号
List.SetItemText(nItem, 1, strName); //姓名
List.SetItemText(nItem, 2, strAge); //年龄
List.SetItemText(nItem, 3, strGender); //性别
List.SetItemText(nItem, 4, strIDNumber); // 身份证号码
List.SetItemText(nItem, 5, strAddress); //地址
List.SetItemText(nItem, 6, strModifyTime); //最后修改时间
List.SetItemText(0, 1, "姓名"); //
List.SetItemText(0, 2, "年龄"); //
List.SetItemText(0, 3, "性别"); //
……先区分出是什么问题引起的