将SQL数据库附加到ComboBox.ItemSource(WPF) [英] Attach a SQL database to ComboBox.ItemSource (WPF)

查看:119
本文介绍了将SQL数据库附加到ComboBox.ItemSource(WPF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将SQL Server数据库分配给ComboBox的ItemSource属性(在WPF应用程序中)。我将数据源分配给了项目,但不知道如何分配给属性。

I want to know how can I assign a SQL Server database to ItemSource property of a ComboBox (in a WPF app). I assigned the data source to the project but do not know how to assign to the property.

最诚挚的问候

推荐答案

您可以尝试如下操作。.您可以像下面这样绑定combobox的item source属性。

you can try like this ..you can bind the item source property of combobox like this below..


ItemsSource = {Binding}

ItemsSource="{Binding}"

EDIT:

连接字符串:

您可以在控件事件或类中添加,但它应该在wpf应用程序窗口中。

You can add in control event or class but it should be in wpf application window.

如果您在Visual Studio或Visual C#中创建新的应用程序,或者创建了window1.xaml的任何应用程序。您基本上需要在该window1.xaml的类或事件中添加连接字符串,而不是在app.config或app.xaml中。

If you create new application in visual studio or visual c# or whatever it creates window1.xaml. you need to add connection string basically in class or event in that window1.xaml not in app.config or app.xaml.

在类中定义连接字符串:

这里是创建一个类的示例(它的sql连接器代替了我在第一个示例中显示的OleDb):

Here is example by creating a class (its sql connector instead of OleDb which i showed in 1st one):

public class ConnectionHelper
{
    public static SqlConnection GetConnection()
    {
        string connectionStr = "Data Source=MICROSOFT-JIGUO;Initial Catalog=CompanyTestDB;Integrated Security=True";
        SqlConnection conn = new SqlConnection(connectionStr);
        return conn;
    }
}

,您可以在方法中使用此类: / p>

and you can use this class in your methods:


  SqlConnection conn = ConnectionHelper.GetConnection();




    <Window
.......
Loaded="OnLoad"
>

<Grid>

<ComboBox Height="18" SelectionChanged="cmbCategory_SelectionChanged" 
    ItemsSource="{Binding}" 
HorizontalAlignment="Right" Margin="0,92,17,0" Name="cmbCategory" 
    VerticalAlignment="Top" Width="176" 
BorderBrush="#FFFFFFFF" SelectedIndex="0"/>

</Grid>
</Window>

在加载功能上,您可以将值分配给组合框

on load function u can assign values to combobox

private void OnLoad(object sender, System.EventArgs e) 
{          
       ListCategories();
}

private void ListCategories()
{
 sqlCon = new SqlConnection();
 sqlCon.ConnectionString = Common.GetConnectionString();
 cmd = new SqlCommand();
 cmd.Connection = sqlCon;
 cmd.CommandType = CommandType.Text;
 cmd.CommandText = "SELECT * FROM Categories";
 sqlDa = new SqlDataAdapter();
 sqlDa.SelectCommand = cmd;
 ds = new DataSet();
 try
 {
     sqlDa.Fill(ds, "Category");
     DataRow nRow = ds.Tables["Category"].NewRow();
     nRow["CategoryName"] = "List All";
     nRow["CategoryID"] = "0";
     ds.Tables["Category"].Rows.InsertAt(nRow, 0);

     //Binding the data to the combobox.
      cmbCategory.DataContext = ds.Tables["Category"].DefaultView;

    //To display category name (DisplayMember in Visual Studio 2005)
      cmbCategory.DisplayMemberPath = 
          ds.Tables["Category"].Columns["CategoryName"].ToString();
    //To store the ID as hidden (ValueMember in Visual Studio 2005)
      cmbCategory.SelectedValuePath = 
          ds.Tables["Category"].Columns["CategoryID"].ToString();

  }
  catch (Exception ex)
  {
      MessageBox.Show("An error occurred while loading categories.");
  }
  finally
  {
      sqlDa.Dispose();
      cmd.Dispose();
      sqlCon.Dispose();
  }

}

这篇关于将SQL数据库附加到ComboBox.ItemSource(WPF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆