【问题标题】:Filtering DataGridView via multiple combo boxes通过多个组合框过滤 DataGridView
【发布时间】:2018-12-11 07:27:28
【问题描述】:

我目前正在开发一个 Windows 窗体应用程序项目,以将 excel 文件数据导入数据网格并应用基于组合框的过滤器。我完成了数据的导入。我相信下一部分应该在按钮中应用过滤代码进行过滤,这是我目前卡住的地方。提前致谢。

My windows form App

用于导入和显示数据的代码片段。

private void btnOpen_Click(object sender, EventArgs e)
    {
        try
        {
            string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
            OleDbConnection con = new OleDbConnection(constr);
            OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + dropdown_sheet.SelectedValue + "]", con);
            sda.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                dataGridView.DataSource = dt;
            }

            comboBox1.Items.Clear();
            foreach (DataGridViewRow row in dataGridView.Rows) 
            {
                comboBox1.Items.Add(row.Cells[0].Value.ToString());
                comboBox2.Items.Add(row.Cells[1].Value.ToString());
                comboBox3.Items.Add(row.Cells[2].Value.ToString());
                comboBox4.Items.Add(row.Cells[3].Value.ToString());
                comboBox5.Items.Add(row.Cells[4].Value.ToString());
                comboBox6.Items.Add(row.Cells[5].Value.ToString());
                comboBox7.Items.Add(row.Cells[6].Value.ToString());
                comboBox8.Items.Add(row.Cells[7].Value.ToString());
                comboBox9.Items.Add(row.Cells[8].Value.ToString());
                comboBox10.Items.Add(row.Cells[9].Value.ToString());
                comboBox11.Items.Add(row.Cells[10].Value.ToString());
            }

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

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "Excel Files | *.xlsx; *.xls; * .xlsm";

            if (openfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tb_path.Text = openfile.FileName;
            }

            string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
            OleDbConnection con = new OleDbConnection(constr);
            con.Open();

            dropdown_sheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            dropdown_sheet.DisplayMember = "TABLE_NAME";
            dropdown_sheet.ValueMember = "TABLE_NAME";

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

【问题讨论】:

  • "...我被卡住了..." - 这些信息并没有真正的帮助。它没有说明究竟是什么不起作用,即您是否在任何地方收到任何错误消息,您是否尝试调试问题以确保它确实完成了预期的事情。
  • 对不起,关于卡住我的意思是我不知道如何继续代码。代码运行良好,导入 excel 文件可以正常工作,我可以在 dataGridView 中看到数据。

标签: c# excel winforms datagridview


【解决方案1】:

通过阅读以下文章,您将找到一种在 datagridview 上实现过滤 UI 的非常好的方法。

这是一个非常好的开始学习的地方,它可以适应其他类型的过滤 UI(例如用于搜索条件的文本框而不是组合框)。

MSDN - Building a Drop-Down Filter List for a DataGridView Column Header Cell

【讨论】:

  • 谢谢,非常有用的文章能够通过使用 dv.RowFilter 找到并解决问题。我现在正在学习如何使程序避免空值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-15
相关资源
最近更新 更多