2015-09-24 97 views
0

我必須使用DataTable和linq填充DataGrid。 DataTable包含一個MySql表(我使用linq來優化程序的網絡流量),但是當linq試圖從DataTable獲取布爾值時,我得到一個「InvalidCastException」exc。與「'OneWay'或'TwoWay'binging不能工作...」文本。有什麼辦法可以使它工作嗎? (SRY壞ENG)使用linq從DataTable獲取布爾值

  string q = "Select * from `beszallitoi_megrendeles` " 
        + "where megrendelt='1' and beerkezett='0' " 
        + "order by megrendeles_datuma desc;"; 
      parancs = new MySqlCommand(q, Kapcsolat); 
      Kapcsolat.Open(); 
      parancs.ExecuteNonQuery(); 

     MySqlDataAdapter mda = new MySqlDataAdapter(parancs); 
     DataTable dt = new DataTable("beszallitoi_megrendeles"); 
     mda.Fill(dt); 
     mda.Update(dt); 
... 
     var results = from a in dt.AsEnumerable() 
         select new 
         { 
          AZ = a.Field<int>("AZ"), 
          MEGRENDEL = a.Field<DateTime>("MEGRENDEL"), 
          KERTSZDATUM = a.Field<DateTime>("KERTSZDATUM"), 
          VEVO_CSOPORT = a.Field<string>("VEVO_CSOPORT"), 
          ROVIDVEVONEV = a.Field<string>("ROVIDVEVONEV"), 
          GYARTO = a.Field<string>("GYARTO"), 
          MEGNEVEZES = a.Field<string>("MEGNEVEZES"), 
          DARAB = a.Field<int>("DARAB"), 
          MEGJEGYZES = a.Field<string>("MEGJEGYZES"), 
          RENDSZAM = a.Field<string>("RENDSZAM"), 
          BRENDSZAM = a.Field<string>("BRENDSZAM"), 
          ROGNEV = a.Field<string>("ROGNEV"), 
          BESZALLITO = a.Field<string>("BESZALLITO"), 
          MEGREND = a.Field<DateTime>("MEGREND"), 
          VARERK = a.Field<DateTime>("VARERK"), 
          CSKULD = a.Field<string>("CSKULD"), 
          MEGJEGY2 = a.Field<string>("MEGJEGY2"), 
          BMEGREND = a.Field<bool>("BMEGREND"), 
          BERKDAT = a.Field<DateTime>("BERKDAT"), 
          BEERK = a.Field<bool>("BEERK") 
         }; 
     DgUjMegrendeles.ItemsSource = results; 

編輯: 這裏是布爾列:(我修改了模式從「雙向」到「單向」,然後「一次性」但這樣一來所有行了真值)

<DataGridCheckBoxColumn Width="45" Header="MREND." Binding="{Binding BMEGREND, Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"/> 
+0

什麼是確切的例外? – SLaks

+0

我認爲它是DBNull那裏,但當然我們需要確切的異常 – Spawn

+0

附加信息:TwoWay或OneWayToSource綁定不能用於「<> f__AnonymousType0'19 [System.Int32,System.DateTime,System.DateTime,System.String,System。串,System.String,System.String,System.Int32,System.String,System.String,System.String,System.String,System.String,的System.DateTime,System.DateTime的,System.String,System.String, System.Int32,System.Int32]「類型的只讀」BMEGREND「屬性。 – Dephott

回答

1

您創建的匿名類型(new { AZ = ... })具有隻讀屬性。您正在嘗試使用綁定模式綁定到某些東西。您必須將綁定模式設置爲OneWayOneTime。但是,您不顯示您的XAML。

+0

我創建了一個名爲「Lista」的自動屬性(選擇新的Lista(){..}),並以這種方式使用「TwoWay」模式。謝謝! – Dephott