trrrrrrm
trrrrrrm

Reputation: 11802

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 ?

Upvotes: 1

Views: 6144

Answers (1)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84647

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}"
          ...>

Upvotes: 3

Related Questions