从c#.net中的Sql DB添加组合框项目 [英] to add combo box items from Sql DB in c#.net

查看:74
本文介绍了从c#.net中的Sql DB添加组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将项目从sqlDB添加到组合框。在我的表单中有1个组合框(对于项目)和1个文本框(对于值).i需要加载项目到应该根据组合框的值选择DB表和文本框的值。



例如:

i need to add the items to combo box from sqlDB.In my form am having 1 combo box(for items) and 1 textbox (for values).i need to load items to both from DB table and the value of textbox should be selected according to value of combo box.

eg:

combobox    textbox
  Items      price
  sss         100
  ddd         140
  fff         220



选择组合框值时,应自动选择文本框值。




The textbox value should be selected automatically when combobox value is selected.

private void Form4_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("data source=PC\\SQLEXPRESS;integrated security=true;Initial catalog=MyDB");
            BindData1();
}
 public void BindData1()
        {
            

            con.Open();
            string strCmd = "select Items from tblbill";
            SqlCommand cmd = new SqlCommand(strCmd, con);
            SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cmd.ExecuteNonQuery();
            con.Close();

            comboBox1.DisplayMember = "items";
            comboBox1.ValueMember = "items";
            comboBox1.DataSource = ds.Tables[0];

            comboBox1.Enabled = true;

        }

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            con.Open();
           string sel = comboBox1.SelectedItem.ToString();
            
            
           
           SqlCommand cmd = new SqlCommand("select price from tblbill where items=" + comboBox1.SelectedValue, con);
            
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                p = dr[0] as string;
                //department = reader[1] as string;
                
            }
            
            textBox1.Text = p;
            con.Close();
        }





我的组合框中填充了来自tblbill的项目,但无法根据选择在文本框中显示价格值在组合框中。

请帮助我...



My combobox is filled with items from tblbill but can't able to display price value in textbox according to selection in combobox.
Please help me...

推荐答案

您有两个不同的问题需要解决:(1)加载内容从数据库,和(2)根据组合框的当前选择更新文本框。



根据您的UI环境,您可能使用可以将集合作为其数据源的组合框。如果是这种情况,那么您的数据库代码应该返回一些集合实体(例如DataTable或List< mycustomtype>并且您可以将其传递给组合框。然后您告诉组合框集合类型的成员是显示和值成员。显示成员是组合框用户在列表中看到的内容;值是代码可以使用的值。



现在,处理'选择已更改'或类似事件。当该事件触发时,从组合框的值(或选定值或其他)属性更新文本框。
You have two distinct problems you are trying to solve: (1) loading content from a database, and (2) updating a text box according to the current selection of the combobox.

Depending on your UI environment, you may have the use of a combobox that can take a collection as its data source. If this is the case then your DB code should return some collection entity (such as a DataTable, or List<mycustomtype> and you can pass that to the combobox. You then tell the combo box what members of the collection type are the display and value members. The display member is what the combobox user sees in the list; the value is what the code can use.

Now, handle the 'selection changed' or similar event. When that event fires, update your text box from the combobox's value (or selected value, or whatever) property.


这篇关于从c#.net中的Sql DB添加组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆