无法在3个组合框之间创建关系

时间:2019-04-17 21:26:27

标签: c#

我需要在2到3个组合框之间建立关系。它在1和2之间比较好,可以比较卖方和商品,但不能在2和3之间比较,应该比较商品的代码和卖方的代码以显示有关数据库大小的信息。

我已经尽力像这部影片一样

https://www.youtube.com/watch?v=_WbWiUGw2DQ

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace abc
{
    /// <summary>
    /// Description of Form5.
    /// </summary>
    public partial class Form5 : Form
    {
            String conn_string = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Users\\Голубь\\Desktop\\бд.mdb;Persist Security Info=False";
            OleDbConnection conn = null;
            List<Postaw> postaws = new List<Postaw>();
            List<Prays> prayses = new List<Prays>();
            List<Razm> razms = new List<Razm>();

        public Form5()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //

        }

        void Label1Click(object sender, EventArgs e)
        {

        }
        void Button2Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void Form5Load(object sender, EventArgs e)
        {
    try 
            {               
                conn = new OleDbConnection(conn_string);
                conn.Open();
                var dataSet = new DataSet();
                OleDbCommand cmd = new OleDbCommand("select * from Поставщики", conn);
                OleDbDataReader rd = cmd.ExecuteReader(); 
                while (rd.Read())
                {
                    comboBox1.Items.Add(rd["Название"]);
                    postaws.Add(new Postaw()
                                {
                                    kodpost = ((int) rd["Код поставщика"]),
                                    nazwpost = rd["Название"] as string
                                });
                }
                conn.Close();


                conn.Open();
                OleDbCommand cmd1 = new OleDbCommand("select *, Товары.Название from Прейскурант inner join Товары on Прейскурант.[Код товара] = Товары.[Код товара]", conn);
                OleDbDataReader rd1 = cmd1.ExecuteReader(); 
                while (rd1.Read())
                {
                    prayses.Add(new Prays()
                         {
                                    kodpreys = ((int) rd1["Код прейскуранта"]), 
                                    nazwtow = rd1["Название"] as string,
                                    kodpost = ((int) rd1["Код поставщика"])
                                    });
                }
                conn.Close();

                conn.Open();
                OleDbCommand cmd2 = new OleDbCommand("select *, Артикул.Размер from Товары inner join Артикул on Товары.[Код товара] = Артикул.[Код товара]", conn);
                OleDbDataReader rd2 = cmd2.ExecuteReader(); 
                while (rd2.Read())
                {
                    razms.Add(new Razm()
                         {
                                    kodtow = ((int) rd2["Код товара"]), 
                                    nazwtow = rd2["Название"] as string,
                                    kodart = ((int) rd2["Код артикула"]),
                                    razm = rd2["Размер"] as string,
                                    });
                }
                conn.Close();


            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private string[] gettowbyid(int kodpreys)
        {
            return prayses.Where(line=>line.kodpost==kodpreys).Select(l=>l.nazwtow).ToArray();
        }

        private string[] getrazbyid(int kodart)
        {
            return razms.Where(line=>line.kodtow==kodart).Select(l=>l.razm).ToArray();
        }


        void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            int kodpreys = prayses[comboBox1.SelectedIndex].kodpreys; 
            foreach(string Название in gettowbyid(kodpreys))
            {
                this.comboBox2.Items.Add(Название);
            }
        }
        void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
        {
    try 
            {               
                conn = new OleDbConnection(conn_string);
                conn.Open();
                var dataSet = new DataSet();
                OleDbCommand cmd = new OleDbCommand("select *, Товары.Название from Прейскурант inner join Товары on Прейскурант.[Код товара] = Товары.[Код товара] where Товары.[Название] = '"+ comboBox2.Text +"'", conn);
                OleDbDataReader rd3 = cmd.ExecuteReader();
                while (rd3.Read())
                {
                    textBox1.Text = ((int) rd3["Цена"]).ToString();

                }
                conn.Close();
                    }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

            comboBox3.Items.Clear();
            int kodart = razms[comboBox2.SelectedIndex].kodart; 
            foreach(string Размер in getrazbyid(kodart))
            {
                this.comboBox3.Items.Add(Размер);
            }


        }
        void Label5Click(object sender, EventArgs e)
        {

        }

    }
    [Serializable]
    class Postaw
        {
            public int kodpost { get; set; }
            public string nazwpost { get; set; }
        }

    [Serializable]
    class Prays
        {
            public int kodpreys { get; set; }
            public string nazwtow { get; set; }
            public int kodpost { get; set; } 
        }

    [Serializable]
    class Razm
        {
            public int kodtow { get; set; }
            public string nazwtow { get; set; }
            public int kodart { get; set; }
            public string razm { get; set; }        
        }

    }

我猜这是行不通的,因为找不到合适的代码,我该如何解决?

0 个答案:

没有答案