简体   繁体   English

数据集加载WPF组合框

[英]Dataset Loading a WPF Combobox

DataSet dataSet = new DataSet();
using (SqlConnection connection = new SqlConnection("server=server;   database=database; user id=user; password=user"))
{
     connection.Open();
     using (SqlCommand command = new SqlCommand("SELECT DISTINCT ID FROM TABLE ORDER BY ID ASC", connection))
     {
          SqlDataAdapter reader = new SqlDataAdapter(command);
          reader.Fill(dataSet);
          IDComboBox.DataContext = dataSet; --> This doesn't work
          IDComboBox.Itemsource = dataSet.Tables[0].Columns[0].ToString() --> doesn't work
          IDComboBox.Itemsource = dataSet.Tables[0].Rows[0].ToString() --> doesn't work
          IDComboBox.Itemsource = dataSet.Tables[0].Rows --> doesn't work
          IDComboBox.Itemsource = dataSet.Tables[0].Columns --> doesn't work
          They don't work even with me pairing it the IDComboBox.DataContext = dataSet.Tables[0].Rows[0] or Columns[0]
     }
     connection.Close();
     connection.Dispose();
}

I am needing to fill my combobox in a WPF with the data from my datatable. 我需要用数据表中的数据在WPF中填充组合框。 All I keep finding are the examples that use Combobox.Displaymember, Combobox.Source to do this but a C# WPF application doesn't have these options. 我一直发现的示例都是使用Combobox.Displaymember,Combobox.Source来执行此操作的示例,但C#WPF应用程序没有这些选项。 How can I load a WPF combobox with data from a dataset or a datatable? 如何使用来自数据集或数据表的数据加载WPF组合框?

One way that I was doing it before was 我以前做过的一种方式是

  using (SqlConnection connection = new SqlConnection("server=server; database=database; user id=user; password=user"))
  {
      connection.Open();
      using (SqlCommand command = new SqlCommand("SELECT DISTINCT ID FROM Table ORDER BY ID ASC", connection))
      {
           SqlDataReader reader = command.ExecuteReader();
           while (reader.Read())
           {
               for (int i = 0; i < reader.FieldCount; i++)
               {
                   IDComboBox.Items.Add(reader[i].ToString());
               }
           }
       }
       connection.Close();
       connection.Dispose();
  }

I know having it for looped into my combobox is very slow if I have large amounts of data so I am wanting to dump it in from a dataset to reduce run time. 我知道,如果我有大量数据,将其循环到我的组合框中非常慢,因此我想将其从数据集中转储以减少运行时间。

Derived from this example , you'll want to work with the ItemSource , DisplayMemberPath , and SelectedValuePath properties: 从此示例派生,您将需要使用ItemSourceDisplayMemberPathSelectedValuePath属性:

IDComboBox.ItemsSource = dataSet.Tables[0].DefaultView;
IDComboBox.DisplayMemberPath = dataSet.Tables[0].Columns["ID"].ToString();
IDComboBox.SelectedValuePath = dataSet.Tables[0].Columns["ID"].ToString();

And in xml : 并在xml

<ComboBox  Name="IDComboBox" ItemsSource="{Binding}"/> 

WPF ComboBox具有您可以使用的ItemSource属性。

IDComboBox.ItemsSource = dataSet.Tables[0].Rows;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM