简体   繁体   English

从数据库获取多个值,并在索引更改C#时显示在组合框和不同的文本框中

[英]get multiple values form the database and show in combo box and different text boxes when the index change c#

   try
        {
           // var model =Execute.q"SELECT product.id, product.productName, product.unitId, product.productCode, units.name FROM product INNER JOIN units ON product.unitId = units.id";

            string query1 = "SELECT product.id, product.productName, product.unitId, product.productCode, units.name FROM product INNER JOIN units ON product.unitId = units.id";
            command = DBConnectivity.getCommandForQuery(query1, connection);
            dataset = new DataSet();
            adapter = new SqlDataAdapter(command);
            adapter.Fill(dataset);
            int reslut = command.ExecuteNonQuery();
            this.cBproductName.DisplayMember = "productName";
            this.cBproductName.ValueMember = "id";
            datarow = dataset.Tables[0].NewRow();
            datarow["productName"] = "Select Product";
            dataset.Tables[0].Rows.InsertAt(datarow, 0);
            this.cBproductName.DataSource = dataset.Tables[0];
        }

        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);

        }
 private void cBproductName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.cBproductName.SelectedValue != null)
        {
            txtProCode.Text = this.cBproductName.SelectedValue.ToString();

        }
    }

i want to show product code and the unit name in the text boxes against the id in the product table and on the change on selected index in the combo box cBproductName. 我想在文本框中针对产品表中的ID和组合框cBproductName中所选索引的更改显示产品代码和单元名称。 my query give me all the values but unable to show in the form. 我的查询为我提供了所有值,但无法在表单中显示。

You can change the SQL statement to include the informations you need. 您可以更改SQL语句以包含所需的信息。

Like: 喜欢:

SELECT product.productName + ' ' + product.productCode + ' ' + units.name  AS ShowText, product.id, product.productName, product.unitId, product.productCode, units.name FROM product INNER JOIN units ON product.unitId = units.id

Change cBproductName_SelectedIndexChanged method to: 将cBproductName_SelectedIndexChanged方法更改为:

DataRow row = this.cBproductName.SelectedItem as DataRow;
if (row != null)
{
  txtProCode.Text = row["ShowText"].ToString();
}

The ComboBox i bound to a DataSet that contains DataRow elements. 我将ComboBox绑定到包含DataRow元素的DataSet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM