-1

I have a c# form. I have a ComboBox that is the controller. I want to populate labels to display fields from the same row as the selected item in my drop down list.

for example, items is my ComboBox, so if I select an item from my ComboBox, then I want "description" to populate my label, as item ComboBox is selected.

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;  //Dir used to connect to sql DB
using System.Data.SqlClient; //Dir used to connect to sql DB



namespace items_Form
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'syteLine_AppDataSet.item' table. You can move, or remove it, as needed.
            this.itemTableAdapter.Fill(this.syteLine_AppDataSet.item);
            //creating the connection to database.
            SqlConnection con_str = new SqlConnection("Data Source=SL1;Initial Catalog=SyteLine_App;Integrated Security=True");
            SqlDataAdapter dta_ad = new SqlDataAdapter("SELECT item, description, lead_time, product_code, p_m_t_code, stocked, matl_type, family_code, low_level, days_supply, order_min, order_mult, plan_code,accept_req, shrink_fact, var_lead, decifld1, decifld2, decifld3, datefld, pass_req, order_max, uf_Active, uf_BoughtOff, uf_Old, uf_ProjectEngineer FROM dbo.item", con_str);
            DataTable dta_tbl = new DataTable();

            dta_ad.Fill(dta_tbl);


            for (int i = 0; i < dta_tbl.Rows.Count; i++)

            {
                cbxItem.Items.Add(dta_tbl.Rows[i]["item"]);
            }

         }           

        public void cbxItem_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (cbxItem.SelectedIndex == -1)
            {
                label1.Text = string.Empty;
            }
            else
            {
                label1.Text = cbxItem.SelectedItem.ToString();
            }
        }
    }
}
4

1 回答 1

1

我只会在页面加载时检索 1 或 2 列,只是为了显示下拉列表。

我将在传递所选值的 cbxItem_SelectedIndexChanged 方法中进行第二次查询:

DataTable dt = SomeFunctionThatGetsTheDataForTheSelectedValue(cbxItem.SelectedValue)
lblDescription = dt.Rows[0]["Description"];
于 2013-02-04T22:27:29.500 回答