如何在将Excel文件加载到文件夹后过滤datagridview [英] How to filter a datagridview by a textbox after loading an excel file into it

查看:152
本文介绍了如何在将Excel文件加载到文件夹后过滤datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中非常乞讨。我正在为客户制定优惠计划。我获得了第一个提供的表单和一个加载到datagridview的excel文件的子表单。当我双击该行时,它将被复制到第一行的datagridview。工作正常但是我想添加一个TextBox与TextChanged事件,所以当用户放置一些文本的datagridview(第二个形式,那个与excel数据)应该只显示包含这个文本的行。如果不可能一次搜索所有的列表就可以了。但我不能让它工作。我发现一些解决方案,我复制了它,但我不工作。所以这里是代码:

  private void button1_Click(object sender,EventArgs e)
{
try
{
System.Data.OleDb.OleDbConnection PolaczenieBazyDanych = new System.Data.OleDb.OleDbConnection(provider = Microsoft.ACE.OLEDB.12.0; Data Source ='C:\\Users\ \user\\Desktop\\My Dropbox\\Cenniki\\\Cennik.xlsx';扩展属性= Excel 8.0;);
System.Data.DataSet DtSet = new System.Data.DataSet();
System.Data.OleDb.OleDbDataAdapter Adapter = new System.Data.OleDb.OleDbDataAdapter(select * from [Tabelle1 $],PolaczenieBazyDanych);
//Adapter.TableMappings.Add(\"Tabela,TabelaTestowa);
Adapter.Fill(DtSet);
dataGridView1.DataSource = DtSet.Tables [0];
PolaczenieBazyDanych.Close();

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

private void textBox1_TextChanged(object sender,EventArgs e)
{
DataView dv =((DataTable)dataGridView1.DataSource).DefaultView ;
dv.RowFilter =FromColumn like'%+ textBox1.Text +%';
dataGridView1.DataSource = dv;


}


解决方案

p>这样做:

  BindingSource bs = new BindingSource(); 
bs.DataSource = dataGridView1.DataSource;
bs.Filter = sColumnaDoPrzeszukania +like'%+ textBox1.Text +%';
dataGridView1.DataSource = bs;


I am at the VERY beggining with C#. I am creating a program for making offers for clients. I got first form with offer and a subform with an excel file loaded into datagridview. When I double click the row it is copied to the datagridview in the first row. It works fine. But I want to add a TextBox with the TextChanged event so when user puts some text there a datagridview (in second form, that one with excel data) should show only rows that contain this text. If it is not possible to search all calumns at once it's fine. But I cannot make it work. I found some sollution and i copied it but i doesn't work. So here's the code:

private void button1_Click(object sender, EventArgs e)
{
  try
  {
    System.Data.OleDb.OleDbConnection PolaczenieBazyDanych = new System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\Users\\user\\Desktop\\My Dropbox\\Cenniki\\Cennik.xlsx';Extended Properties=Excel 8.0;");
    System.Data.DataSet DtSet = new System.Data.DataSet();
    System.Data.OleDb.OleDbDataAdapter Adapter = new System.Data.OleDb.OleDbDataAdapter("select * from [Tabelle1$]", PolaczenieBazyDanych);
    //Adapter.TableMappings.Add("Tabela", "TabelaTestowa");
    Adapter.Fill(DtSet);     
    dataGridView1.DataSource = DtSet.Tables[0];
    PolaczenieBazyDanych.Close();

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

private void textBox1_TextChanged(object sender, EventArgs e)
{
  DataView dv = ((DataTable)dataGridView1.DataSource).DefaultView;
  dv.RowFilter = "FromColumn like '%" + textBox1.Text + "%'";
  dataGridView1.DataSource = dv;


}

解决方案

This did work:

  BindingSource bs = new BindingSource();
  bs.DataSource = dataGridView1.DataSource;
  bs.Filter = sColumnaDoPrzeszukania + " like '%" + textBox1.Text + "%'";
  dataGridView1.DataSource = bs;

这篇关于如何在将Excel文件加载到文件夹后过滤datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆