Reputation: 331
I have this search function, in order to flag comments from MySQL database that matched a list of keywords, flagged comments will be displayed on the dataGridView_flaggedComments
, then follow by populating comboBox_stockIndex
with the involved share price symbol (e.g. BARC, LLOY, TSCO).
private void button_Search1_Click(object sender, EventArgs e)
{
commentCount = 0;
dataGridView_flaggedComments.Refresh();
DataTable flaggedcomments = new DataTable("flaggedcomments");
using (MySqlConnection sqlConn = new MySqlConnection(strProvider))
{
using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT Comment_ID, Comments_Date, Comments_Time, Author, Title, Comments_Comment, Tickers_Ticker_ID FROM comments ORDER BY Comments_Date ASC, Comments_Time 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();
PopulateStockIndex();
}
private void PopulateStockIndex()
{
comboBox_stockIndex.Items.Clear();
comboBox_stockIndex.Items.Add("Choose to Filter");
DataTable link_stockIndex = new DataTable("link_stockIndex");
using (MySqlConnection sqlConn = new MySqlConnection(strProvider))
{
using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT Ticker_ID, Symbol FROM tickers", sqlConn))
{
da.Fill(link_stockIndex);
}
}
foreach (DataRow da in link_stockIndex.Rows)
{
for (int i = 0; i < dataGridView_flaggedComments.Rows.Count - 1; i++)
{
if (dataGridView_flaggedComments.Rows[i].Cells["Tickers_Ticker_ID"].Value.ToString() != "" && dataGridView_flaggedComments.Rows[i].Cells["Tickers_Ticker_ID"].Value.ToString() == da["Ticker_ID"].ToString())
{
if (!comboBox_stockIndex.Items.Contains(da[1].ToString()))
{
comboBox_stockIndex.Items.Add(da[1].ToString());
}
comboBox_stockIndex.SelectedIndex = 0;
}
}
}
}
Next, if I select a symbol from the comboBox_stockIndex
, the dataGridView_flaggedComments
should be filtered to show only the comments relevant to the selected symbol (once symbol is selected, it will look for the symbol's Tickers_TTicker_ID, then filter by the Tickers_Ticker_ID). But the below code wouldn't work. There's an error saying "Object reference not set to an instance of an object." for this line (dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter = string.Format("Tickers_Ticker_ID = '{0}'", da["Ticker_ID"]);
. I tried to debug, but I don't understand what went wrong.
private void comboBox_stockIndex_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable link_stockIndex = new DataTable("link_stockIndex");
using (MySqlConnection sqlConn = new MySqlConnection(strProvider))
{
using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT Ticker_ID, Symbol FROM tickers", sqlConn))
{
da.Fill(link_stockIndex);
}
}
foreach (DataRow da in link_stockIndex.Rows)
{
for (int i = 0; i < dataGridView_flaggedComments.Rows.Count - 1; i++)
{
if (dataGridView_flaggedComments.Rows[i].Cells["Tickers_Ticker_ID"].Value.ToString() != "" && comboBox_stockIndex.SelectedItem.ToString() == da["Symbol"].ToString())
{
(dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter = string.Format("Tickers_Ticker_ID = '{0}'", da["Ticker_ID"]);
}
}
}
}
I've been spending two days looking into this, SOF is my last resort. Any help would be very very much appreciated! Thank you very much!
Upvotes: 2
Views: 2660
Reputation: 11820
DataView
is not DataTable
.
Here you setting DataSoure
as DataView
:
dataGridView_flaggedComments.DataSource = query.AsDataView();
Here you casting it as DataTable
:
(dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter =...
So if I inderstood quickly your problem, solution would be:
(dataGridView_flaggedComments.DataSource as DataView).RowFilter =...
Next time add breakpoint to the place where you getting this kind of error. Look at what boxed type you have and to what type you trying to unbox.
Also I would not suggest to use as
like you do. Better do like so:
DataView dv = dataGridView_flaggedComments.DataSource as DataView;
if(dv == null)
throw new Exception("Bad Data Source type");
else
{
//use dv here
}
Upvotes: 3
Reputation: 26896
As from your code: dataGridView_flaggedComments.DataSource = query.AsDataView();
So when you are trying to call this: (dataGridView_flaggedComments.DataSource as DataTable)
you will get null
, because your DataSource
is not DataTable
but DataView
.
It looks like you should use something like (dataGridView_flaggedComments.DataSource as DataView).RowFilter
instead.
Upvotes: 2