从数据库中取出如下多条记录,目标:在combobox中显示A1,A2...,选择A1后,取得B1值,A1,B1都为字符串.A1 B1
A2 B2
A3 B3
...==================================
int i=0;
_variant_t var;
CString strA,strB;
CComboBox m_Box;while(!m_pRecordset->ADOEOF)
{
var = m_pRecordset->GetCollect("A");
if(var.vt != VT_NULL)
strA = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("B");
if(var.vt != VT_NULL)
strB = (LPCSTR)_bstr_t(var); /////////////////
//就是这这里(还有取值的时候)不知道该怎么写?
i = m_CBox.AddString(strA);
////////////////
m_pRecordset->MoveNext();
}
==================================
A2 B2
A3 B3
...==================================
int i=0;
_variant_t var;
CString strA,strB;
CComboBox m_Box;while(!m_pRecordset->ADOEOF)
{
var = m_pRecordset->GetCollect("A");
if(var.vt != VT_NULL)
strA = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("B");
if(var.vt != VT_NULL)
strB = (LPCSTR)_bstr_t(var); /////////////////
//就是这这里(还有取值的时候)不知道该怎么写?
i = m_CBox.AddString(strA);
////////////////
m_pRecordset->MoveNext();
}
==================================
解决方案 »
- visual studio 2010中的类向导
- MFC鼠标滚轮怎样与窗口滚动条关联?
- 想用vc做一个小的搜索程序的话用到什么知识?比如求职搜索.自动抓取一些网站的求职信息之类的
- 关于实现统计访问数据库人数的问题
- 在MFC中,实现文件选择的“打开文件选择”对话框出错,麻烦帮看一下错在什么地方!
- 如何从数据库中取出时间
- 各种指针 本质上不都是一样的么?为什么 作为函数参数 和 赋值 时,不能自动强制?
- 怎样加入背景
- 如何导入工具条(将另一个程序中的一个工具条资源拷贝到另一个程序)30
- 这5个错误怎么也解决不了了
- 如何在一个ListBox里添加数据
- 怎么打不开工程的resources文件?总是提示“load failed”
在检索数据库,得到B1
CString strText;
m_CBox.GetLBText(nItem, strText);
那么我在给CComboBox m_Box赋值时代码该怎么写呢?
是的,我希望能只用一个CComboBox.wuxfBrave:
我比较菜,有时间的话能给出具体点的代码嘛?谢谢.
然后将用SetItemData函数将B1加入到A1项目的附加存储空间中。
这样A1和B1就通过列表的索引值对应起来。
楼主的代码不变,在取出一条记录A和对应的B后,A显示在ComboBox中,同时B保存在CStringList类表中。在得到A1后,CStringList中查找相应的B1。
在可见的那个里选 a1则得到索引值,对应不可见的那个combox,就的到b1
我同意他的说法,好像的用SetItemData和GetItemData
m_Capture.SetItemData(i,strB);但是编译出错:cannot convert parameter 2 from 'class CString' to 'unsigned long'
请问该怎么解决啊?
m_Capture.SetItemData(i,(LPCTSTR)strB);
E:\VC\ADOComboBox\ADOComboBoxDlg.cpp(249) : error C2664: 'SetItemData' : cannot convert parameter 2 from 'const char *' to 'unsigned long'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
CStringList m_strBs;在前面的代码中加入:
i = m_Box.AddString(strA);
m_strBs.AddTail(strB);选择不同的A值时:
void CADOComboBoxDlg::OnSelchange()
{
int i;
i = m_Box.GetCurSel();
POSITION pos;
if((pos = m_strBs.FindIndex(i)) != NULL)
{ m_strB = m_strBs.GetAt(pos);}
UpdateData(FALSE);
}