使用c#在windows窗体中级联组合框 [英] cascading combobox in windows forms using c#

查看:507
本文介绍了使用c#在windows窗体中级联组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 sql server database 中有一个名为 Item 的表。物品表定义是



 Item_Code Item_Name Item_Make Item_Price UnitofMeasurement 

电缆锚 45 0000
Cable polycab 30 0000
按钮锚 15 0000 unit
Button arells 20 0000 unit
Switch cona 70 0000 单位





我有2个组合框,第一个Combobox用于Item_Name 第二个用于Item_Make 。我希望第二个组合框在第一个Combobox选择的索引已更改上填充Item_Make。不幸的是我无法实现这一目标。



我尝试了什么:



  private   void  comboBoxName_SelectedIndexChanged( object  sender,EventArgs e)
{
if (!string.IsNullOrEmpty(comboBoxName.Text))
{
fillMake ();
comboBoxMake.SelectedIndex = -1;
}

}

private void fillName()
{
string str = 从Item;中选择不同的Item_Name;
使用(SqlConnection con = new SqlConnection( @ 数据源= ashish-pc\;初始目录= HMS;综合安全性=真))
{
使用(SqlCommand cmd = new SqlCommand(str,con))
{
使用(SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataTable dtItem = new DataTable();
adp.Fill(dtItem);

comboBoxName.DataSource = dtItem;
comboBoxName.DisplayMember = Item_Name;
comboBoxName.ValueMember = Item_Name;
}
}
}
}
私有 void fillMake()
{
使用(SqlConnection con = new SqlConnection( @ Data Source = ashish-pc\; Initial Catalog = HMS; Integrated Security = True))
{
string str = 从项目中选择Item_Make,其中Item_Name = @ Item_Name;
使用(SqlCommand cmd = new SqlCommand(str,con))
{
cmd.Parameters.AddWithValue( @ Item_Name,comboBoxName.Text);
使用(SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataTable dtItem = new DataTable();
adp.Fill(dtItem);
comboBoxMake.ValueMember = Item_Make;
comboBoxMake.DisplayMember = Item_Make;
comboBoxMake.DataSource = dtItem;

}
}
}

}





请帮帮我

解决方案

您是否使用断点查看。您在此处获得所选项目名称。



 cmd.Parameters.AddWithValue(  @ Item_Name ,comboBoxName.Text); 





试试这样



 cmd.Parameters.AddWithValue(  @ Item_Name,comboBoxName.Selecteditem.Tostring ()); 


I have a table named Item in sql server database. Item table definition is

Item_Code  Item_Name  Item_Make Item_Price UnitofMeasurement

           Cable        anchor  45.0000       meter
           Cable        polycab 30.0000       meter
           Button       anchor  15.0000       unit
           Button       havells 20.0000       unit
           Switch       cona    70.0000       unit



I have 2 combobox, 1st Combobox for Item_Name and 2nd for Item_Make. I want 2nd combobox to be filled with Item_Make on 1st Combobox selected Index changed. Unfortunately I was unable to achieve this.

What i have tried :

private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(comboBoxName.Text))
            {
                fillMake();
                comboBoxMake.SelectedIndex = -1;
            }           
            
        }

       private void fillName()
       {
           string str = "Select distinct Item_Name from Item";
           using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
           {
               using (SqlCommand cmd = new SqlCommand(str, con))
               {
                   using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                   {
                       DataTable dtItem = new DataTable();
                       adp.Fill(dtItem);
                       
                       comboBoxName.DataSource = dtItem;
                       comboBoxName.DisplayMember = "Item_Name";
                       comboBoxName.ValueMember = "Item_Name";
                   }
               }
           }
       }
       private void fillMake()
       {
            using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
            {
                string str = "Select Item_Make from Item Where Item_Name=@Item_Name";
                using (SqlCommand cmd = new SqlCommand(str, con))
                {
                    cmd.Parameters.AddWithValue("@Item_Name", comboBoxName.Text);
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        DataTable dtItem = new DataTable();
                        adp.Fill(dtItem);
                        comboBoxMake.ValueMember = "Item_Make";
                        comboBoxMake.DisplayMember = "Item_Make";
                        comboBoxMake.DataSource = dtItem;

                    }
                }
            }
           
       }



Please help me

解决方案

Did you check using break point.Did you get your selected item Name over here.

cmd.Parameters.AddWithValue("@Item_Name",comboBoxName.Text);



try like this

cmd.Parameters.AddWithValue("@Item_Name",comboBoxName.Selecteditem.Tostring());


这篇关于使用c#在windows窗体中级联组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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