“对象引用未设置为对象的实例"过滤dataGridView时 [英] "Object reference not set to an instance of an object" when filtering dataGridView
问题描述
我具有此搜索功能,为了标记来自MySQL数据库的与关键字列表匹配的注释,标记的注释将显示在 dataGridView_flaggedComments
上,然后填充 comboBox_stockIndex 代码>,其中包含涉及的股价符号(例如BARC,LLOY,TSCO).
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;
}
}
}
}
接下来,如果我从 comboBox_stockIndex
中选择一个符号,则应过滤 dataGridView_flaggedComments
以仅显示与所选符号相关的注释(一旦选择了该符号,它将将查找该符号的Tickers_TTicker_ID,然后按Tickers_Ticker_ID进行过滤).但是下面的代码不起作用.出现错误消息对象引用未设置为对象的实例".对于此行(dataGridView_flaggedComments.DataSource作为DataTable).DefaultView.RowFilter= string.Format("Tickers_Ticker_ID ='{0}'",da ["Ticker_ID"]);
.我尝试调试,但是不知道出了什么问题.
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"]);
}
}
}
}
我已经花了两天时间研究这个问题,SOF是我的最后选择.任何帮助将不胜感激!非常感谢你!
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!
推荐答案
DataView
不是 DataTable
.
此处将 DataSoure
设置为 DataView
:
dataGridView_flaggedComments.DataSource = query.AsDataView();
此处将其强制转换为 DataTable
:
(dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter =...
因此,如果我快速了解您的问题,解决方案将是:
So if I inderstood quickly your problem, solution would be:
(dataGridView_flaggedComments.DataSource as DataView).RowFilter =...
下一次将断点添加到您遇到这种错误的地方.查看您拥有的装箱类型以及要尝试取消装箱的类型.另外,我不建议像您一样使用 as
.最好这样做:
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
}
这篇关于“对象引用未设置为对象的实例"过滤dataGridView时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!