如何在组合框中检索显示成员 [英] how to retrieve display member in combobox
问题描述
HELLO FRIENDS ........
这是我的代码
用于组合框.....
HELLO FRIENDS...........
here is my code
for combobox.....
<pre lang="c#">
现在在另一个功能
我想通过EXCODE ....
并希望找到ITS关联的Exname
now at another function
i want pass EXCODE.......
and want to find ITS associate Exname
public void fillexcombo()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//SqlConnection con = Connection1.getopenedconnection();
SqlCommand cmd = new SqlCommand("SELECT EXNAME,EXCODE FROM EXMAST where compcode='" + Compcls.Gcomp_cd + "'", con);
SqlDataReader sdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Clear();
dt.Load(sdr);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = dt.Columns[0].ColumnName;
comboBox1.ValueMember = dt.Columns[1].ColumnName;
con.Close();
}
这是我的代码....
//*********************************************
here id my code....
//*********************************************
public void itemmodify()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("SELECT ITEMNAME,EXHCODE,LOT,PRICEUNIT,REGULARLOT,QTYUNIT,DELIVERYLOT,DELIVERYUNIT,ACTIVE,EXCHANGECODE FROM ITEMMAST WHERE ITEMCODE='" + textBox1.Text + "'AND compcode='" + Compcls.Gcomp_cd + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds1 = new DataSet();
sda.Fill(ds1, "ITEMMAST");
textBox1.Enabled = false;
textBox2.Text = ds1.Tables[0].Rows[0]["ITEMNAME"].ToString();
textBox5.Text = ds1.Tables[0].Rows[0]["EXHCODE"].ToString();
textBox7.Text = ds1.Tables[0].Rows[0]["LOT"].ToString();
textBox8.Text = ds1.Tables[0].Rows[0]["PriceUnit"].ToString();
textBox11.Text = ds1.Tables[0].Rows[0]["Regularlot"].ToString();
textBox10.Text = ds1.Tables[0].Rows[0]["Qtyunit"].ToString();
textBox12.Text = ds1.Tables[0].Rows[0]["Deliverylot"].ToString();
textBox9.Text = ds1.Tables[0].Rows[0]["deliveryunit"].ToString();
y = ds1.Tables[0].Rows[0]["ACTIVE"].ToString();
if (y == "0")
{
radioButton2.Checked = true;
}
else if (y=="1")
{
radioButton1.Checked = true;
}
//********************************************************************************
string Excodee = ds1.Tables[0].Rows[0]["EXCHANGECODE"].ToString();
at this position i have Excodee now Want to display Exname from cobbobox1
what i have to do??????????????????
//*************************************************************************************
con.Close();
推荐答案
string a=comboBox1.Text
//that's all you will get the display member value which you have selected
希望这对您有帮助
Hope this will help you
我这样做的方法是创建代表数据库信息的对象的集合
the way I do it is to create a collection of objects that represent the databasase info
public class MyObject
{
public string property1 { get; set; }
public int property2 { get; set; }
public MyObject(DataRow row)
{
property1 = (row != null) ? row("column1") : "UNK";
property2 = (row != null) ? row("column2") : 0;
}
public override string ToString()
{
return property1;
}
}
public class MyObjectCollection
{
public MyObjectCollection(DataTable table)
{
if (table != null && table.HasRows)
{
for (int i = 0; i < table.Rows.Count; i++)
{
this.Add(new MyObject(table.Rows[i]);
}
}
}
}
此时,您要做的就是:
At this point, all you have to do is this:
MyObjectCollection myObjects = new MyObjectCollection(table);
comboBox1.DataSource = myObjects;
// or
comboBox1.Items.AddRange(myObjects.ToArray);
...,然后组合框将显示property1,而您无需执行任何操作.更好的是,当您想要选定项目的数据时,只需执行以下操作:
...and the combobox will display property1 without you having to do anything. Even better, when you want the data for the selected item, you just do this:
MyObject myObject = comboBox1.SelectedItem as MyObject;
您最终得到的是从面向外部的代码中提取的大多数复杂性,这意味着该程序更具可维护性.
What you end up with is most of the complexity abstracted away from the outward facing code, which means the program is MUCH more maintainable.
这篇关于如何在组合框中检索显示成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!