我目前正在Windows窗体应用程序项目中,将Excel文件数据导入数据网格并基于组合框应用过滤器。数据导入已完成。我相信下一部分应该在按钮中应用过滤代码进行过滤,这就是我目前遇到的问题。预先感谢。
用于导入和显示数据的代码片段。
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);
}
}
答案 0 :(得分:0)
通过阅读以下文章,您将找到一种非常好的方法来在datagridview上实现筛选UI。
这是一个开始学习某些东西的好地方,它可以适应其他类型的筛选UI(例如,用于搜索条件的文本框而不是组合框)。
MSDN - Building a Drop-Down Filter List for a DataGridView Column Header Cell