我怎么能一个列表框绑定到数据表中的WPF应用程序/ [英] How can I bind a listbox to a data table in WPF applications/

查看:105
本文介绍了我怎么能一个列表框绑定到数据表中的WPF应用程序/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一列表的内容到列表框。我用这个code做到这一点。

  listBox1.DataContext = ds.Tables [0];

我也试过用这个code

  listBox1.ItemsSource = ds.Tables [0];

和两人都没有工作。

任何想法,我该怎么办呢?

 命名空间itinventory
    {
       ///<总结>
      ///为MainWindow.xaml交互逻辑
      ///< /总结>
     公共部分类主窗口:窗口
     {
         SqlConnection的CON;
         SqlDataAdapter的DA;
         数据集DS;        公共主窗口()
        {
            的InitializeComponent();
            CON =新的SqlConnection(服务器= MYSERVER \\\\ sqlex preSS;+=数据库ITINVENTORY;用户ID = SA;+密码=输入mypassword);
            con.Open();
        }        私人无效的button1_Click(对象发件人,RoutedEventArgs E)
        {
            SqlDataAdapter的大=新SqlDataAdapter的(选择员工DISTINCT组织code,CON);
            DataSet的DS =新的DataSet();
            da.Fill(DS,组织code);
            listBox1.DataContext = ds.Tables [0];
        }
    }
}


解决方案

在这里进行了优化codeS和一些suggessions你。

 字符串constring = @数据源= \\ SQLEX $ P $干燥综合征; AttachDbFilename ='D:\\ TEMPX \\项目x \\ ODataClient \\ ODataClient \\ DATA \\ NORTHWND.MDF'。集成安全=真;用户实例=真;
        无效的button1_Click(对象发件人,RoutedEventArgs E)
        {
            DataTable的DT =新的DataTable();
            使用(SqlConnection的CON =新的SqlConnection(constring))
            {
                con.Open();
                SqlDataAdapter的大=新SqlDataAdapter的(从员工*,CON);                da.Fill(DT);
                con.Close();
            }            listBox1.ItemsSource = dt.AsDataView();
        }

我不会告诉你该怎么写查询。但对于你的问题在​​这里有几点。

如果您使用的是绑定的DataContext然后是确定的。在DataContext意味着你路过它可以进一步绑定到其子属性的对象。你NEEC绑定ListBox的ItemsSource属性。为

=的ItemsSource绑定{}

 < ListBox的X:名称=listBox1中>
            < ListBox.ItemTemplate>
                <&DataTemplate的GT;
                    < TextBlock的文本={结合姓}/>
                < / DataTemplate中>
            < /ListBox.ItemTemplate>
        < /列表框>

但如果你只是路过的数据列出你需要做的是asssing数据为ItemsSource属性是什么,这将只显示显示数据的对象类型的全名insteed。使用上面的模板中显示的数据。

另一件事是,当你assing DataTable对象到itemsssource / DataContext的源必须是Enumrable所以以此为数据视图。自IList,IEnumrable其insherited。等等。

I am trying to put the content of the one column table into the listbox. I use this code to do that.

listBox1.DataContext = ds.Tables[0];

I also tried to use this code

listBox1.ItemsSource = ds.Tables[0];

and both didn't work.

Any idea how can I do that?

    namespace itinventory
    {
       /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
     public partial class MainWindow : Window
     {
         SqlConnection con ;
         SqlDataAdapter da ;
         DataSet ds;

        public MainWindow()
        {
            InitializeComponent();
            con = new SqlConnection("Server=myserver\\sqlexpress;" + "Database=ITINVENTORY;User ID=sa;" + "Password=mypassword");
            con.Open();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("select DISTINCT orgcode from Employees", con);
            DataSet ds = new DataSet();
            da.Fill(ds,"orgcode");
            listBox1.DataContext = ds.Tables[0];
        }
    }
}    

解决方案

here is optimized codes and some suggessions for you.

string constring = @"Data Source=.\SQLEXPRESS;AttachDbFilename='D:\TEMPX\Projects X\ODataClient\ODataClient\Data\northwnd.mdf';Integrated Security=True;User Instance=True";
        void button1_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(constring))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter("select * from Employees", con);

                da.Fill(dt);
                con.Close();
            }

            listBox1.ItemsSource = dt.AsDataView();
        }

i will not tell you about the how to write the queries. but for your question here are some points.

If you are using binding then dataContext is ok. The DataContext means you are passing a object which can be further bindable to its child properties. and you neec to bind the ItemsSource Property of ListBox. as

ItemsSource={Binding}.

 <ListBox x:Name="listBox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FirstName}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

But if you are just passing the Data to List what you need to do is asssing data into ItemsSource property and that will only show the object type Full Name insteed of showing data. Use above template to show data.

another thing is that when you assing DataTable object to a itemsssource/ DataContext the source must be Enumrable so use this as DataView. its insherited from Ilist , IEnumrable. etc.

这篇关于我怎么能一个列表框绑定到数据表中的WPF应用程序/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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