DataView RowFilter不会过滤DataGridView上的行 [英] DataView RowFilter doesn't filter the Rows on DataGridView

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

问题描述

我有这个功能 button_Search1_Click 来搜索匹配关键字的评论,然后在 dataGridView_flaggedComments 中显示这些标记的注释。

I have this function button_Search1_Click to search for comments that match keywords, then display these flagged comments in dataGridView_flaggedComments.

接下来,如果 comboBox_stockIndex 中有任何更改,我希望过滤器发生,即过滤标记的评论在 dataGridView_flaggedComments 中, Tickers_Ticker_ID 1 。但是当我这样做时,所有的评论(不论是否被标记)都属于 Tickers_Ticker_ID 1 显示在我的 dataGridView_flaggedComments 。它应该只显示 Tickers_Ticker_ID 标记的评论 1 ,而不是所有的评论。

Next, if there's any changes on the comboBox_stockIndex, I want the filter to take place i.e. filter the flagged comments in dataGridView_flaggedComments with the Tickers_Ticker_ID of 1. But when I do that, all the comments (regardless flagged or not) belong to Tickers_Ticker_ID of 1 display on my dataGridView_flaggedComments. It should have only display the flagged comments for Tickers_Ticker_ID of 1, not all the comments.

我认为 DataSource 有问题,但我无法理解。任何帮助将非常感谢!谢谢!

I think there's something wrong with the DataSource but I couldn't figure it out. Any help would be very very much appreciated! Thank you!

(如果我错过了任何类似的问题,请指出,非常感谢!)

(If I did miss any similar questions, kindly point it out. Thank you very much!)

private void button_Search1_Click(object sender, EventArgs e)
{
    commentCount = 0;
    richTextBox_flaggedComments.Clear();
    dataGridView_flaggedComments.Refresh();
    DataTable flaggedcomments = new DataTable("flaggedcomments");
    using (MySqlConnection sqlConn = new MySqlConnection(strProvider))
    {
        using (MySqlDataAdapter da = new MySqlDataAdapter(
        "SELECT Comment_ID, Comments_Date, Author, Title, Comments_Comment, " + 
              " Tickers_Ticker_ID FROM comments ORDER BY Comments_Date ASC", sqlConn))
        {
            da.Fill(flaggedcomments);
        }
    }
    StringBuilder sb = new StringBuilder();
    string[] words = File.ReadAllLines(sourceDirTemp + 
                          comboBox_crimeKeywords.SelectedItem.ToString() + ".txt");
    var query = flaggedcomments.AsEnumerable().Where(r =>
        words.Any(wordOrPhrase => Regex.IsMatch(r.Field<string>("Comments_Comment"),
              @"\b" + Regex.Escape(wordOrPhrase) + @"\b",  RegexOptions.IgnoreCase)));

    dataGridView_flaggedComments.DataSource = query.AsDataView();
}


private void comboBox_stockIndex_SelectedIndexChanged(object sender, EventArgs e)
{
    DataView dv = dataGridView_flaggedComments.DataSource as DataView;
    if (dv == null)
        throw new Exception("Bad Data Source type");
    else
    {
        dv.RowFilter = string.Format("Tickers_Ticker_ID = '1'");
        dataGridView_flaggedComments.DataSource = dv;
    }
}


推荐答案

DataView 因此不会持有任何数据。

设置过滤器时正在通过新的过滤器将新的过滤器替换为 LinqDataView 中的原始过滤器,即 Where 子句, RowFilter

When you set the filter you are effectively replacing the orginal filter in your LinqDataView, that is the Where clause, by the new filter, that is by the RowFilter.

您需要连接它们才能创建双重条件。

You need to concatenate them to create a double condition.

由于您的 Where 子句使用复杂的 RegEx 认为最简单的方法是重新使用它,使用新的简单的'Tickers_Ticker_ID ='+ id 条件来附加。

Since your Where clause uses a complex RegEx I think the easiest way will be to re-use it, appending it with the new, simple 'Tickers_Ticker_ID = ' + id condition.

如果您不想重新应用原始过滤器,您可能希望将过滤的行存储在临时表中。这里我有一个DataSet DS,首先克隆第一个表的结构,命名新的表,并将其添加到DataSet。在适当的情况下,从查询中复制过滤的行:

If you don't want to reapply the original filter you may want to store the filtered rows in a temporary Table. Here I have a DataSet DS and first clone the structure of the 1st Table, name the new Table and add it to the DataSet. When appropriate I copy the filtered rows over from the query:

设置临时表,您在哪里设置其他数据库:

Set up the Temp Table, where you set up you other DB stuff:

DataSet DS;                             // if you don't already have one..
                                        // put it at class level!

DS = new DataSet();                     // ..create it
DataTable DT = DS.Tables[0].Clone();    // the temp table has the sdame structure
DT.TableName = "temp";                  // is called by a name
DS.Tables.Add(DT);                      // and (optionally) added to the DataSet.

当您进行搜索时,将数据加载到临时表中:

When you do the search you load the data into the temp table:

DS.Tables["temp"].Rows.Clear();
query.CopyToDataTable( DS.Tables["temp"], LoadOption.OverwriteChanges);
DGV.DataSource = DS.Tables["temp"]; 

现在您可以在 combo_filter_SelectedIndexChanged 事件:

 string id = ddl_filter.Text;
 if (id == "") DGV.DataSource = DS.Tables["temp"];
 else
 {
    DataView dv = new DataView(DS.Tables["temp"])
    dv.RowFilter = string.Format("id = " + id) ;
    DGV.DataSource = dv;
 }

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

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