wpf:如何在网格视图中显示数据集?

时间:2021-09-23 06:23:50

i'm moving from windows form to wpf but now i have a problem.

我正在从Windows窗体移动到wpf,但现在我遇到了问题。

i get info from database(sql server) and store that in a dataset and i want to show that in a datagrid (dg)

我从数据库(sql server)获取信息并将其存储在数据集中,我想在数据网格中显示(dg)

DataSet ds = new DataSet();
SqlConnection sc = new SqlConnection("mysqlconnection");
SqlDataAdapter sd = new SqlDataAdapter();
sc.Open();
sd.SelectCommand = new SqlCommand("SELECT * FROM table_1", sc);
sd.Fill(ds);
dg.DataContext = ds.Tables[0].DefaultView;//here is the problem
sc.Close();

in windows forms it was dg.DataSrouce but i can't find that in wpf, any help ?

在Windows窗体中它是dg.DataSrouce,但我在wpf中找不到任何帮助?

1 个解决方案

#1


3  

Either add ItemsSource="{Binding}" to your DataGrid definition or change

将ItemsSource =“{Binding}”添加到DataGrid定义或更改

dg.DataContext = ds.Tables[0].DefaultView;

to

dg.ItemsSource = ds.Tables[0].DefaultView;

Update
Try to add AutoGenerateColumns="True"

更新尝试添加AutoGenerateColumns =“True”

<DataGrid Name="dg" 
          AutoGenerateColumns="True"
          ItemsSource="{Binding}"
          ...>

#1


3  

Either add ItemsSource="{Binding}" to your DataGrid definition or change

将ItemsSource =“{Binding}”添加到DataGrid定义或更改

dg.DataContext = ds.Tables[0].DefaultView;

to

dg.ItemsSource = ds.Tables[0].DefaultView;

Update
Try to add AutoGenerateColumns="True"

更新尝试添加AutoGenerateColumns =“True”

<DataGrid Name="dg" 
          AutoGenerateColumns="True"
          ItemsSource="{Binding}"
          ...>