【问题标题】:filter datagridview rows过滤 datagridview 行
【发布时间】:2012-05-11 00:11:22
【问题描述】:

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

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];                
}

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

【问题讨论】:

    标签: c# text datagridview filter


    【解决方案1】:

    在将 Table 分配给 resultGrid.DataSource 之前,可以使用 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);
    

    或者你可以使用DataView:

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

    【讨论】:

      【解决方案2】:

      我们可以使用绑定源来过滤数据。

       private BindingSource dashBoardBindingSource = new BindingSource();
       dashBoardBindingSource.DataSource= dataSet.Tables[0]; 
       resultsGrid.DataSource=dashBoardBindingSource;
      
       string filterCriteria = "CustomerFirstName={0}";
       dashBoardBindingSource.Filter=string.Format(CultureInfo.CurrentCulture, filterCriteria, textboxName.text);
      

      【讨论】:

        猜你喜欢
        • 2018-04-08
        • 2010-12-02
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多