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接口的,所以可以进行赋值。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
相关资源
相似解决方案