gpt4 book ai didi

c# - "Object reference not set to an instance of an object"过滤dataGridView时

转载 作者:行者123 更新时间:2023-12-02 00:30:38 27 4
gpt4 key购买 nike

我有这个搜索功能,为了标记MySQL数据库中与关键字列表匹配的评论,标记的评论将显示在dataGridView_flaggedComments上,然后填充 comboBox_stockIndex以及相关的股价代码(例如 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 as DataTable).DefaultView.RowFilter = string.Format("Tickers_Ticker_ID = '{0}'", da["Ticker_ID"]); 。我尝试调试,但不明白出了什么问题。

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是我最后的手段。任何帮助将非常非常感谢!非常感谢!

最佳答案

DataView 不是 DataTable

此处将DataSoure设置为DataView:

dataGridView_flaggedComments.DataSource = query.AsDataView();

在这里将其转换为DataTable:

(dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter =...

所以,如果我很快理解你的问题,解决方案是:

(dataGridView_flaggedComments.DataSource as DataView).RowFilter =...

下次在出现此类错误的地方添加断点。查看您拥有的装箱类型以及您尝试拆箱的类型。另外,我不建议像您一样使用 as 。最好这样做:

DataView dv = dataGridView_flaggedComments.DataSource as DataView;
if(dv == null)
throw new Exception("Bad Data Source type");
else
{
//use dv here
}

关于c# - "Object reference not set to an instance of an object"过滤dataGridView时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26608836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com