我该怎么解决这个系统.invalidcastexception未处理? [英] How can I solve this system.invalidcastexception was unhandled?
问题描述
我有一个WPF DataGrid来显示PostgreSQL数据。它显示一个包含14行的数据表。它是服务器的外部连接,列数根据用户参数不断变化。我创建了一个名为UCS1freqs的类,我用它来设置数据的频率值,以便稍后将这些值绘制成散点图。问题是,一旦它收集了所有数据,它就会给我一个无效的强制转换异常未处理(无法将类型为
MS.Internal.NamedObject的对象转换为类型System.Data.DataRowView)。我知道问题与我使用DataRowView有关。我是新手代码,对WPF来说肯定是新手。非常感谢任何帮助
我尝试过:
var ucs1Freqs = new List
I have a WPF DataGrid to display PostgreSQL data. It displays a data table with 14 rows. It is an external connection to server and the number of columns change constantly depending on the user parameters. I have created a class called UCS1freqs and I use it to set the frequency values from data for later plotting those values into a scatter chart. The problem is that once it collects all the data it gives me a invalid cast Exception was unhandled (Unable to cast object of type
MS.Internal.NamedObject to type System.Data.DataRowView). I know the problem is related to me using DataRowView. I'm new to code and definitely new to WPF. Any help is greatly appreciated
What I have tried:
var ucs1Freqs = new List
推荐答案
这是我的问题代码
var ucs1Freqs = new List< Ucs1Freq>();
foreach(DataGrid1.Items中的DataRowView行)
{
if(转换) .ToDouble(row.Row.ItemArray [11])== 28.464258839)
{
var Ucs1freqstring = row.Row.ItemArray [0] .ToString(); < br $>
row.Row.ItemArray [0] = Ucs1freqstring;
Ucs1Freq tempFreq = new Ucs1Freq();
tempFreq.Frequency = Ucs1freqstring;
ucs1Freqs.Add(tempFreq);
}
}
this is the code from my question
var ucs1Freqs = new List<Ucs1Freq>();
foreach (DataRowView row in DataGrid1.Items)
{
if (Convert.ToDouble(row.Row.ItemArray[11]) == 28.464258839)
{
var Ucs1freqstring = row.Row.ItemArray[0].ToString();
row.Row.ItemArray[0] = Ucs1freqstring;
Ucs1Freq tempFreq = new Ucs1Freq();
tempFreq.Frequency = Ucs1freqstring;
ucs1Freqs.Add(tempFreq);
}
}
这篇关于我该怎么解决这个系统.invalidcastexception未处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!