问题
I have this function button_Search1_Click
to search for comments that match keywords, then display these flagged comments in dataGridView_flaggedComments
.
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.
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;
}
}
回答1:
A DataView
as such does not hold any data.
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.
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.
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"];
Now you can use it in the combo_filter_SelectedIndexChanged
event:
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;
}
回答2:
Your filtering wrong... try this instead...
dv.RowFilter = "Tickers_Ticker_ID = 1";
来源:https://stackoverflow.com/questions/26706066/dataview-rowfilter-doesnt-filter-the-rows-on-datagridview