简体   繁体   中英

wpf : how to show a data set in a grid view?

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

i get info from database(sql server) and store that in a dataset and i want to show that in a datagrid (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 ?

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

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

to

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

Update
Try to add AutoGenerateColumns="True"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM