过滤datagridview行 [英] filter datagridview rows

查看:101
本文介绍了过滤datagridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个datagridview已经有很多很多行添加到它。我还有一个文本框,如果它们不匹配,我想要从datagridview行中过滤这些行。我以某种方式想要将文本框连接到一列以显示和隐藏行。该表填充使用:

  public void readExcelFile()
{
string connectionString =Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ Properties.Settings.Default.excelFilePath +;扩展属性= \Excel 8.0; HDR = Yes; IMEX = 1\;;

string query = String.Format(select * from [{0} $],Batch Export);

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query,connectionString);

DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);

resultsGrid.DataSource = dataSet.Tables [0];
}

现在,当我开始在文本框中输入John,第一列是名称,当我删除字符时,我想要的所有行的名称单元格不以J开头,然后是乔等,我想让行回来。这可能吗?在使用SQL查询到DB之前,我已经做到了,但这个来源是不同的。而且我必须清除每个textChanged事件的表。这里有大约20k行。

解决方案

在将表分配给resultGrid.DataSource之前,可以使用DataTable.Select:

  var strExpr =CustomerFirstName ='John'AND OrderCount> 2; 
var strSort =OrderCount DESC;

//使用Select方法查找与过滤器匹配的所有行。
foundRows = ds.Table [0] .Select(strExpr,strSort);

或者您可以使用DataView:

  ds.Tables [0] .DefaultView.RowFilter = strExpr; 


Hello I have a datagridview that already has many, many rows added to it. I also have a textbox that I want to filter the rows out of the datagridview rows if they do not match. I somehow want to connect the textbox to a column to show AND hide the rows. The table was populated using:

public void readExcelFile()
{
    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + Properties.Settings.Default.excelFilePath + "; Extended Properties = \"Excel 8.0;HDR=Yes;IMEX=1\";";

    string query = String.Format("select * from [{0}$]", "Batch Export");

    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet);

    resultsGrid.DataSource = dataSet.Tables[0];                
}

Now when I start typing "John" into the textbox and the first column is "Name", I want all the rows whose Name cell doesn't start with "J", then "Jo", etc. when I delete characters, I want the rows to come back. Is this possible? I've done this before using SQL queries to a DB, but this source is different. And I had to clear the table on every textChanged event. There are about 20k rows here.

解决方案

Before you assign the Table to the resultGrid.DataSource, you can use DataTable.Select:

var strExpr = "CustomerFirstName = 'John' AND OrderCount > 2";
var strSort = "OrderCount DESC";

// Use the Select method to find all rows matching the filter.
foundRows = ds.Table[0].Select(strExpr, strSort);

Or you can use DataView:

ds.Tables[0].DefaultView.RowFilter = strExpr;

这篇关于过滤datagridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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