C#Combobox和DataGridView

时间:2017-01-24 14:32:03

标签: c# winforms datagridview combobox

我有一个组合框,它从select和datagridview中检索数据,从另一个查询中检索数据。我想使用comboboxvalue过滤datagridview。我正在尝试一切,但没有任何工作。能否请你帮忙?此外,为什么当我声明dataview =((DataTable)datagridview.datasource.defaultview(combobox_SelectedIndexChanged中的第一行)时,我再也看不到组合框中的任何值,而是看到System Data DataRowView,但第一个问题更重要对我来说

    private void Form5_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True;");
        conn.Open();
        SqlCommand sc = new SqlCommand("  SELECT id, customername+' - '+cast(inserted as varchar(19)) as targ FROM bf where customername>'a' order by customername asc, inserted desc ", conn);
        SqlDataReader reader;

        reader = sc.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("targ", typeof(string));
        dt.Load(reader);


        comboBox1.ValueMember = "id";
        comboBox1.DisplayMember = "targ";
        comboBox1.DataSource = dt;
        conn.Close();


        var select = "SELECT [id],[CustomerName[email],[Capital] FROM baf order by id desc";
        var c = new SqlConnection("Data Source=xxxxxx;Initial Catalog=xxxxx;Integrated Security=True;"); // Your Connection String here
        var dataAdapter = new SqlDataAdapter(select, c);

        var commandBuilder = new SqlCommandBuilder(dataAdapter);
        var ds = new DataSet();
        dataAdapter.Fill(ds);
        dataGridView1.ReadOnly = true;
        dataGridView1.DataSource = ds.Tables[0];

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        //var dataView = ((DataTable)dataGridView1.DataSource).DefaultView;

        //    if (comboBox1.Text == "Remove filter")
        //    {
        //        dataView.RowFilter = string.Empty;
        //    }
        //    else
        //    {
        //        //dataView.RowFilter = "id = {comboBox1.Text}";
        //    }

        //}

    }

3 个答案:

答案 0 :(得分:1)

您需要正确使用BindingSource。以下是完整的工作示例。要试验它你需要的是:

  1. 创建空白表单Q1
  2. 将一个DataGridView控件和一个ComboBox控件添加到窗体
  3. 将代码下方的代码粘贴到Q1.cs文件中
  4. 运行并试验
  5. 我希望这会让你顺利。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WinFormQ
    {
        public partial class Q1 : Form
        {
            public Q1()
            {
                InitializeComponent();
            }
    
            BindingSource bs;
    
            private void Q1_Load(object sender, EventArgs e)
            {
                // SqlConnection conn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True;");
                // conn.Open();
                // SqlCommand sc = new SqlCommand("  SELECT id, customername+' - '+cast(inserted as varchar(19)) as targ FROM bf where customername>'a' order by customername asc, inserted desc ", conn);
                // SqlDataReader reader;
                // 
                // reader = sc.ExecuteReader();
    
                DataTable dt = new DataTable();
                dt.Columns.Add("id", typeof(string));
                dt.Columns.Add("targ", typeof(string));
                // dt.Load(reader);
    
                dt.Rows.Add("1", "Targ-1");  // example code - remove
                dt.Rows.Add("2", "Targ-2");  // example code - remove
                dt.Rows.Add("3", "Targ-3");  // example code - remove
                dt.Rows.Add("4", "Targ-4");  // example code - remove
    
                comboBox1.ValueMember = "id";
                comboBox1.DisplayMember = "targ";
                comboBox1.DataSource = dt;
    
                // var select = "SELECT [id],[CustomerName[email],[Capital] FROM baf order by id desc";
                // var c = new SqlConnection("Data Source=xxxxxx;Initial Catalog=xxxxx;Integrated Security=True;");  Your Connection String here
                // var dataAdapter = new SqlDataAdapter(select, c);
    
                // var commandBuilder = new SqlCommandBuilder(dataAdapter);
    
                DataSet ds = new DataSet();
                DataTable bf = new DataTable("BF");
                bf.Columns.Add("id", typeof(string));  // example code - remove
                bf.Columns.Add("CustomerName", typeof(string));  // example code - remove
                bf.Columns.Add("Email", typeof(string));  // example code - remove
                bf.Columns.Add("Capital", typeof(string));  // example code - remove
    
                ds.Tables.Add(bf);
                bs = new BindingSource(ds, "BF");
                // dataAdapter.Fill(bf);
    
                bf.Rows.Add("1", "Customer-1", "Email-1", "Capital-1");  // example code - remove
                bf.Rows.Add("1", "Customer-2", "Email-2", "Capital-1");  // example code - remove
                bf.Rows.Add("2", "Customer-3", "Email-3", "Capital-2");  // example code - remove
                bf.Rows.Add("3", "Customer-4", "Email-4", "Capital-3");  // example code - remove
                bf.Rows.Add("3", "Customer-5", "Email-5", "Capital-3");  // example code - remove
                bf.Rows.Add("3", "Customer-6", "Email-6", "Capital-3");  // example code - remove
                bf.Rows.Add("4", "Customer-7", "Email-7", "Capital-4");  // example code - remove
                dataGridView1.ReadOnly = true;
                dataGridView1.DataSource = bs;
    
                this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            }
    
            void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.Text == "Remove filter")
                {
                    bs.RemoveFilter();
                }
                else if (comboBox1.SelectedValue == null)
                {
                    bs.RemoveFilter();
                }
                else
                {
                    bs.Filter = "id = " + comboBox1.SelectedValue;
                }
            }
    
        }
    }
    

答案 1 :(得分:0)

我以这种方式解决了(可能不是最优雅但它确实有效):

    private void Form5_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=xxxxx;Integrated Security=True;");
        conn.Open();
        SqlCommand sc = new SqlCommand("  SELECT id, customername+' - '+cast(inserted as varchar(19)) as targ FROM baf where customername>'a' order by customername asc, inserted desc ", conn);
        SqlDataReader reader;

        reader = sc.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("targ", typeof(string));
        dt.Load(reader);


        comboBox1.ValueMember = "id";
        comboBox1.DisplayMember = "targ";
        comboBox1.DataSource = dt;
        conn.Close();



        var select = "SELECT [id],[CustomerName], [email],[CapActual] FROM baf order by id desc";
        var c = new SqlConnection("Data Source=xxxx;Initial Catalog=xxxxx;Integrated Security=True;"); // Your Connection String here
        var dataAdapter = new SqlDataAdapter(select, c);

        var commandBuilder = new SqlCommandBuilder(dataAdapter);
        var ds = new DataSet();
        var bf = new DataTable("BF");
        ds.Tables.Add(bf);

        dataAdapter.Fill(bf);
        dataGridView1.ReadOnly = true;
        dataGridView1.DataSource = bf;






    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {



        var select = "SELECT [id],[CustomerName],[email],[CapActual] FROM baf where id="+comboBox1.SelectedValue+" order by id desc";
        var c = new SqlConnection("Data Source=xxxxx;Initial Catalog=xxxxxx;Integrated Security=True;"); // Your Connection String here
        var dataAdapter = new SqlDataAdapter(select,c);

        var commandBuilder = new SqlCommandBuilder(dataAdapter);
        var ds = new DataSet();
        var bf = new DataTable("BF");
        ds.Tables.Add(bf);

        dataAdapter.Fill(bf);
        dataGridView1.ReadOnly = true;
        dataGridView1.DataSource = bf;










    }

    }




    }

答案 2 :(得分:0)

对于有兴趣找到如何使相关代码成功运行的观众,我将在下面介绍解决方案。为了实现准备,数据是硬编码的,需要根据实际情况进行修改。

    DataView dataView = null; // <<< Difference #1

    private void Form1_Load(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("targ", typeof(string));

        dt.Rows.Add("1", "Targ-1");  // example code - remove
        dt.Rows.Add("2", "Targ-2");  // example code - remove
        dt.Rows.Add("3", "Targ-3");  // example code - remove
        dt.Rows.Add("4", "Targ-4");  // example code - remove

        comboBox1.ValueMember = "id";
        comboBox1.DisplayMember = "targ";
        comboBox1.DataSource = dt;

        var bf = new DataTable("BF");
        bf.Columns.Add("id", typeof(string));  // example code - remove
        bf.Columns.Add("CustomerName", typeof(string));  // example code - remove
        bf.Columns.Add("Email", typeof(string));  // example code - remove
        bf.Columns.Add("Capital", typeof(string));  // example code - remove

        bf.Rows.Add("1", "Customer-1", "Email-1", "Capital-1");  // example code - remove
        bf.Rows.Add("1", "Customer-2", "Email-2", "Capital-1");  // example code - remove
        bf.Rows.Add("2", "Customer-3", "Email-3", "Capital-2");  // example code - remove
        bf.Rows.Add("3", "Customer-4", "Email-4", "Capital-3");  // example code - remove
        bf.Rows.Add("3", "Customer-5", "Email-5", "Capital-3");  // example code - remove
        bf.Rows.Add("3", "Customer-6", "Email-6", "Capital-3");  // example code - remove
        bf.Rows.Add("4", "Customer-7", "Email-7", "Capital-4");  // example code - remove
        dataGridView1.ReadOnly = true;
        dataGridView1.DataSource = bf;

        dataView = bf.DefaultView;  // <<< Difference #2
        this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);  // <<< Difference #3
    }

    void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.Text == "Remove filter")
        {
            dataView.RowFilter = string.Empty;
        }
        else
        {
            dataView.RowFilter = string.Format("id = '{0}'", comboBox1.SelectedValue);   //  <<< Difference #4
        }

    }