WPF DataGrid绑定到数据源的方法


 1 string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["str"].ConnectionString;
 2             SqlConnection con = new SqlConnection(conStr);
 3             SqlCommand command = new SqlCommand();
 4             command.CommandText = "select * from test";
 5             command.Connection = con;
 6             command.CommandType = System.Data.CommandType.Text;
 7             try
 8             {
 9                 con.Open();
10                 SqlDataAdapter sda = new SqlDataAdapter(command);
11                 DataSet ds = new DataSet();
12                 sda.Fill(ds,"Test");
13                 DataTable dt = ds.Tables["Test"];
14                 this.data.ItemsSource = dt.DefaultView;
15             }
16             catch
17             {
18                 MessageBox.Show("error");
19             }

 今天又看到这里,说一下

为什么DataTableDefaultView属性可以赋值给DataGrid,而DataTable不行,

因为DataTableDefaultView属性返回的是一个DataView对象,而DataView对象是实现了IEnumerable接口的,所以可以进行赋值。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM