【问题标题】:filtering datagridview with combobox populated from same database使用从同一数据库填充的组合框过滤 datagridview
【发布时间】:2016-12-11 11:14:37
【问题描述】:

这就是我的代码真正的外观和执行方式。我一直在尝试根据我从数据库中填充的组合框过滤数据,然后在 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;

namespace KPI_Tool
{
    public partial class SearchForm : Form
    {
        static SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\010495\Desktop\KPI_Tool\KPI_Tool\KPI_Store.mdf;Integrated Security=True");      

        public SearchForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
// TODO: This line of code loads data into the 'kPI_StoreDataSet1.Store' table. You can move, or remove it, as needed.
this.myAdapter.Fill(this.myDataSet.Store);

        }



        private void Group_DropDown(object sender, EventArgs e)
        {

       conn.Open();
       comboBox1.Items.Clear();
       SqlCommand cmd = conn.CreateCommand();
       cmd.CommandType = CommandType.Text;
       cmd.CommandText = "SELECT DISTINCT GroupN FROM Store WHERE GroupN IS NOT NULL";
       cmd.ExecuteNonQuery();
       DataTable dt = new DataTable();
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       da.Fill(dt);

       foreach (DataRow dr in dt.Rows)
       {
           comboBox1.Items.Add(dr["GroupN"].ToString());
       }

       conn.Close();


        }

        private void Tech_DropDown(object sender, EventArgs e)
        {

            conn.Open();

            comboBox2.Items.Clear();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT DISTINCT Tech_Area FROM Store WHERE Tech_Area IS NOT NULL";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                comboBox2.Items.Add(dr["Tech_Area"].ToString());
            }

            conn.Close();

        }

        private void Level_DropDown(object sender, EventArgs e)
        {
            conn.Open();

            comboBox3.Items.Clear();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT DISTINCT LevelOf FROM Store WHERE LevelOf IS NOT NULL";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                comboBox3.Items.Add(dr["LevelOf"].ToString());
            }

            conn.Close();
        }

        private void Domain_DropDown(object sender, EventArgs e)
        {
            conn.Open();

            comboBox4.Items.Clear();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT DISTINCT DomainN FROM Store WHERE DomainN IS NOT NULL";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                comboBox4.Items.Add(dr["DomainN"].ToString());
            }

            conn.Close();
        }

        private void Type_DropDown(object sender, EventArgs e)
        {
            conn.Open();

            comboBox5.Items.Clear();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT DISTINCT TypeN FROM Store WHERE TypeN IS NOT NULL";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                comboBox5.Items.Add(dr["TypeN"].ToString());
            }

            conn.Close();
        }

        private void Severity_DropDown(object sender, EventArgs e)
        {
            conn.Open();

            comboBox6.Items.Clear();

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT DISTINCT Severity FROM Store WHERE Severity IS NOT NULL";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);

            foreach (DataRow dr in dt.Rows)
            {
                comboBox6.Items.Add(dr["Severity"].ToString());
            }

            conn.Close();
        }

        private void AlertTB_Click(object sender, MouseEventArgs e)
        {
            AlertTB.Clear();
        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ListB_Click(object sender, EventArgs e)
        {

        }

        private void ClearB_Clicked(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = -1;
            comboBox2.SelectedIndex = -1;
            comboBox3.SelectedIndex = -1;
            comboBox4.SelectedIndex = -1;
            comboBox5.SelectedIndex = -1;
            comboBox6.SelectedIndex = -1;

            AlertTB.Clear();
            AlertTB.Text = "Write Here..";

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {
            conn.Open();
            myBindingSource.Filter = "GroupN= '{0}'"+comboBox1.SelectedItem.Te;
            conn.Close();
        }



        }

    }

这是我的用户界面。我正在使用 Visual Studio Professional 2013。请用非常基本的句子向我解释。我想学习逻辑,代码背后的结构。

【问题讨论】:

    标签: c# visual-studio datagridview combobox filtering


    【解决方案1】:
    public static DataSet SQLGetData(string connectionString, string commandString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            DataSet DS = new DataSet();
            DataTable DT = new DataTable("Table1");
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand(commandString, connection);
                //command.CommandTimeout = 3000;
                SqlDataReader read = command.ExecuteReader();
                DS.Tables.Add(DT);
                DS.Load(read, LoadOption.PreserveChanges, DS.Tables[0]);
    
            }
            catch (SqlException e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
            finally
            {
                connection.Close();
            }
            return DS;
        }
    }
    
    private void SetFilter()
    {
        string command = "SELECT * FROM Store";
    
        int count = 0;
    
        if (comboBox1.Text != "")
        {
            command = command + " WHERE GroupN = '" + comboBox1.Text + "'";
            count = count + 1; 
        }
    
        if (comboBox2.Text != "")
        {
            if (count == 0)
            {
                command = command + " WHERE Tech_Area = '" + comboBox2.Text + "'";                
            }
            else
            {
                command = command + " AND Tech_Area = '" + comboBox2.Text + "'";
            } 
            count = count + 1;                 
        }
    
        if (comboBox3.Text != "")
        {
            if (count == 0)
            {
                command = command + " WHERE LevelOf = '" + comboBox3.Text + "'";                
            }
            else
            {
                command = command + " AND LevelOf = '" + comboBox3.Text + "'";
            }
            count = count + 1;                 
        }
        // comboBox4, comboBox5, comboBox6 
    
        string connStr; //Connection string;
    
        DataSet DS = new DataSet();
        DS = SQLGetData(connStr, command);      
        DataGridView1.DataSource = DS.Tables[0];               
    }
    

    【讨论】:

    • 因为数据适配器不能处理字符串类型,所以应该转换成Text。我们该怎么做?
    【解决方案2】:

    如果你想根据你在文本框中输入的文本过滤gridview你可以参考我的博文

    http://dotnetsolutionsbyankit.blogspot.in/2013/04/filter-gridview-as-you-type-in-textbox.html

    所以你会知道如何去做。

    如果您遇到任何问题,请在评论中告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多