2015-09-28 50 views
0

XAML代碼:組合框表示System.Data.DataRowView在WPF

xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" 
    <xctk:CheckComboBox x:Name="Cb_Blowshell" HorizontalAlignment="Left" Margin="195,9,0,0" Grid.Row="2" VerticalAlignment="Top" Width="267" DisplayMemberPath="prod_name" ValueMemberPath="Id" ItemsSource="{Binding}"/> 

C#代碼

DataTable dt = new DataTable("blow_shell"); 
String Qry = "select Id,prod_name from blow_shell where weight=" + Txt.Text.Trim().ToString() + ""; 
SqlHelper.Fill_dt(dt, Qry); 
Cb_Blowshell.ItemsSource= dt.DefaultView; 

及其簡要代碼以數據表中的數據綁定到組合框;但我的顯示成員顯示System.Data.DataRowView

請幫我解決。

Cb_Blowshell.ItemsSource= dt.DefaultView; 


我猜你想爲每個項目在CheckComboBox內容,以顯示你的數據庫表的prod_name列:

回答

0

使用此行代碼無法綁定數據。

試試這個(以下簡稱「Fill_dt」方法調用之後插入):

List<string> names = new List<string>(); 
for (int i = 0; i < dt.Rows.Count; i++) 
{ 
    String prodName = Convert.ToString(dt.Rows[i]["prod_name"]); 
    names.Add(prodName); 
} 

Cb_Blowshell.ItemsSource = names; 


順便說一句: 你輸出「System.Data.DataRowView」是當您嘗試綁定的一個典型的例子到這種情況下的源類型DataRowView,它不適合目標控件(在您的情況下:CheckComboBox的一個項目)。 在這種情況下,您將看到綁定的源對象的ToString方法的輸出。所以在這裏你可以看到Object基類的「ToString」方法的輸出:this.GetType().ToString();

+0

嘿嘗試這種方法,工作正常,但我希望既顯示成員和值成員。 –

+0

你想要展示什麼?在你的select語句中只有「Id」和「prod_name」。您可以爲這兩個字段創建一個類,創建這些對象的列表並將它們綁定爲項目源。在這種情況下,您必須設置「DisplayMemberPath」。有關詳細信息,請參閱此處:http://stackoverflow.com/a/4902454/4424024 – Martin