How can I solved system.invalidcastexception was unhandled?

Sep 20 2016 11:38 PM

 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

 

   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);
 
         }
 
      }
 
 

Answers (6)