如何基于datagridview外部的组合框在datagridview组合框中过滤结果

时间:2018-12-10 05:09:33

标签: c# datagridview combobox

我试图使用datagridview将多个数据输入到sql中,但是我很难绑定datagridview组合框以显示基于组合框(供应商)中所选项目的项目,我什至无法绑定我的SQL在datagridview组合框中,对不起,我是菜鸟,但我正在尝试完成此项目。下面的代码是我使用的代码,它基于单独的组合框和文本框,未绑定到datagridview。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Inventory.Properties;

namespace Inventory
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        SqlConnection conn = new SqlConnection(@"Data Source=MEDIXPC197;Initial Catalog=Inventory;Integrated Security=True");

        SqlCommand cd = new SqlCommand();

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'inventoryDataSet.dbTrans' table. You can move, or remove it, as needed.
            this.dbTransTableAdapter.Fill(this.inventoryDataSet.dbTrans);
            cc();
            cc2();
            cc3();
        }

        public void cc()
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT Department from tbldept";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            Da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                comboBox1.Items.Add(dr["Department"].ToString());
                comboBox2.Items.Add(dr["Department"].ToString());
            }


            conn.Close();
        }

        public void cc2()
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select distinct Supplier from tblmaster";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            Da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                comboBox3.Items.Add(dr["Supplier"].ToString());
            }
            conn.Close();
        }

        public void cc3()
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT [Transaction] from [tbltransac]";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            Da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                comboBox4.Items.Add(dr["Transaction"].ToString());
            }
            conn.Close();
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from tbldept where Department = '" + comboBox1.SelectedItem.ToString() + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataReader dr = cmd.ExecuteReader();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            conn.Close();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from tbldept where Department = '" + comboBox2.SelectedItem.ToString() + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataReader dr = cmd.ExecuteReader();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            conn.Close();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)

        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from tblmaster where Supplier = '" + comboBox3.SelectedItem.ToString() + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            Da.Fill(dt);

            comboBox5.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                comboBox5.Items.Add(dr["ProductCode"].ToString());
            }
            conn.Close();
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from [tbltransac] where [Transaction] = '" + comboBox4.SelectedItem.ToString() + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            conn.Close();
        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from tblmaster where ProductCode = '" + comboBox5.SelectedItem.ToString() + "'";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter Da = new SqlDataAdapter(cmd);
            Da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                textBox3.Text = dr["Description"].ToString();
                textBox4.Text = dr["UM"].ToString();
                textBox5.Text = dr["UP"].ToString();
            }
            conn.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }

}

编辑: 下图显示了我的设计,我想根据“供应商”组合框过滤datagridview内“项目代码”组合框中显示的内容 PHOTO

0 个答案:

没有答案