我正在尝试填充combobox
表单oracle数据库然后根据
来自combobox
的所选值我想自动填充textbox1
,textbox2
和textbox3
这是我试图用来填充combobox
的代码
第一个问题是combobox
加载为空
第二,我想学习如何自动填充其他textboxs
如果有人可以帮助我或向我解释
public void Fillcombobox()
{
string connstr = "data source=db;user id=user;password=pwd;";
string cmdtxt = "select product_id, description from products";
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt,conn))
{
conn.Open();
OracleDataReader dr;
cmd.ExecuteReader();
CB_PRODUCT_ID.DisplayMember = "product_id";
CB_PRODUCT_ID.ValueMember = "description";
while (dr.Read())
{
// what should I d to fill other textboxs ?
TB_PRODUCTS.Text = ???;
}
}
}
private void CB_PRODUCT_ID_SelectedIndexChanged(object sender, EventArgs e)
{
Fillcombobox();
}
答案 0 :(得分:1)
你必须组合光标中的所有值,例如List<String>
:
public void Fillcombobox()
{
//TODO: do not hardcode connection string (esp. password), but load it
string connstr = "data source=db;user id=user;password=pwd;";
string cmdtxt =
@"select product_id,
description
from products";
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
{
conn.Open();
// reader is IDisposable and should be closed
using (OracleDataReader dr = cmd.ExecuteReader())
{
List<String> items = new List<String>();
while (dr.Read())
{
items.Add(String.Format("{0}, {1}", dr.GetValue(0), dr.GetValue(1)));
}
TB_PRODUCTS.Items.AddRange(items.ToArray());
}
}
}
修改:要更新TextBoxes(textbox1
,textbox2和textbox3),建议您使用Combobox
事件TextChanged
:
private void TB_PRODUCTS_TextChanged(object sender, EventArgs e) {
ComboBox cb = sender as ComboBox;
// Either explicitly or via cb.SelectedIndex
textbox1.Text = cb.Text;
}
答案 1 :(得分:0)
while (dr.Read())
{
TB_PRODUCTS.Text = dr["YourColumnName"].toString();
}