“ DataGrid”不包含“ DataSource”的定义

时间:2018-11-18 08:36:33

标签: c# sql-server database wpf

我在尝试从ComboBox获取所选值以显示在我的DataGrid中时遇到麻烦。首先,我正在使用Systems.Windows.Forms的引用。看完教程后,看来.DataSource无效。

private void CustomerID_TextChanged(object sender, TextChangedEventArgs e)
{
    if (customerCombo.Text == "CUS_ID")
    {
        SqlConnection con = new SqlConnection("Data Source = xmsql04.australiaeast.cloudapp.azure.com,6302 ;Initial Catalog=DAD_TruckRental_RGM;Persist Security Info=True;User ID=DDQ4_Melveena;Password=fBit$73939");
        SqlDataAdapter sda = new SqlDataAdapter("SELECT RentalId,TruckId,CustomerID,TotalPrice FROM Truckrental where CustomerID like'"+CustomerID.Text+"%'", con);

        DataTable dt = new DataTable();

        sda.Fill(dt);
        customerDataGrid.DataSource = dt;
    }

它将显示此错误:

  

“ DataGrid”不包含“ DataSource”的定义,也没有扩展方法“ DataSource”接受第一个   可以找到类型为'DataGrid'的参数(您是否缺少   指令还是程序集引用?)ProjectDAD C:\ Users \ Little   Grace \ Desktop \ ProjectDAD \ RentalManagement \ displayInfo.xaml.cs 73有效

1 个答案:

答案 0 :(得分:1)

在WPF中,它是ItemsSource,在WPF中,您也需要使用DefaultView

customerDataGrid.ItemsSource = dt.DefaultView;