C#组合框关系 [英] C# Combobox Relations

查看:89
本文介绍了C#组合框关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;

首先,我对c#是陌生的,其次,我有一个访问数据库,在此数据库中,我有2种形式,当我使用第一种产品(productid,productname)和第二种ProductData(id,Productid,Model,width,height,price)时,选择productname,我想查看所选产品的型号(每个产品的型号都不同),并查看所选产品(每个型号的宽度和高度都不同)的width,height型号,并查看所选的宽度和高度价格.但我不知道该怎么做:(

感谢您的帮助
最好的问候


这是代码:

Hi ;

first i am new about c# , Second i have a access database , inside this db i have 2 form , first one Products (productid , productname) and second one ProductData(id, Productid , Model , width , height , price ) , when i select productname , i want to see that selected products model (every product have different model ) and see that selected (every model have different width and height ) models width , height and see that selected width and height price. but i dont know how to do it Frown | :(

thanks for helps
best regards


here is the code :

OleDbConnection bag = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb");
OleDbCommand kmt = new OleDbCommand();
DataTable tablo = new DataTable();

private void button1_Click(object sender, EventArgs e)
{
    ListViewItem lvi = new ListViewItem(comboBox1.Text);
    lvi.SubItems.Add(comboBox2.Text);
    lvi.SubItems.Add(comboBox3.Text);
    lvi.SubItems.Add(comboBox4.Text);
    lvi.SubItems.Add(comboBox5.Text);
    lvi.SubItems.Add(comboBox6.Text);
    lvi.SubItems.Add(comboBox7.Text);
    lvi.SubItems.Add(textBox1.Text);
    lvi.SubItems.Add(textBox2.Text);
    listView1.Items.Add(lvi);
    comboBox1.Text = "";
    comboBox2.Text = "";
    comboBox3.Text = "";
    comboBox4.Text = "";
    comboBox5.Text = "";
    comboBox6.Text = "";
    comboBox7.Text = "";
    textBox1.Text = "";
    textBox2.Text = "";


}

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'database1DataSet.UrunData' table. You can move, or remove it, as needed.
    this.urunDataTableAdapter.Fill(this.database1DataSet.UrunData);
    OleDbConnection bag = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb");
    bag.Open();
    comboBox1.Items.Clear();
    OleDbDataReader read;
    kmt.Connection = bag;
    kmt.CommandText = "SELECT Productname FROM Products";
    read = kmt.ExecuteReader();
    while (read.Read())
    {
        comboBox1.Items.Add(read[-1]);
    }
    bag.Close();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}

private void fillByToolStripButton_Click(object sender, EventArgs e)
{
    try
    {
        this.urunDataTableAdapter.FillBy(this.database1DataSet.UrunData);
    }
    catch (System.Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }

}

private void fillBy1ToolStripButton_Click(object sender, EventArgs e)
{
    try
    {
        this.urunDataTableAdapter.FillBy1(this.database1DataSet.UrunData);
    }
    catch (System.Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }

}

推荐答案

尝试使用DataSource属性作为名称组合框.
Try to use DataSource property for your names combobox.
kmt = new System.Data.OleDb.OleDbCommand("SELECT ProductID, Productname FROM Products");
kmt.Connection = bag;
tablo = new DataTable("Products");
System.Data.OleDb.OleDbDataAdapter oda = new System.Data.OleDb.OleDbDataAdapter(kmt);
oda.Fill(tablo);
comboBox1.DataSource = tablo;
comboBox1.ValueMember = "ProductID";
comboBox1.DisplayMember = "ProductName";


然后将以下内容添加到您的comboBox1_SelectedIndexChanged方法:


Then add to your comboBox1_SelectedIndexChanged method the following:

if( !System.Convert.IsDBNull(comboBox1.SelectedValue) ){
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
    string query = "SELECT id, ProductID, Model FROM ProductData\n" +
                   "WHERE ProductID = @pid";
    cmd.Parameters.AddWithValue( "@pid", comboBox1.SelectedValue );
    cmd.CommandType = CommandType.Text
    cmd.CommandText = query;
    cmd..Connection = bag;

    DataTable tbl2 = new DataTable("ProductData");
    System.Data.OleDb.OleDbDataAdapter od = new System.Data.OleDb.OleDbDataAdapter(cmd);
    od.Fill(tbl2);
    // fill combobox or any other control
    comboBox2.DataSource = tbl2;
    comboBox2.ValueMember = "id";
    comboBox2.DisplayMember = "Model";
}


这篇关于C#组合框关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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