开发者

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.Da开发者_JAVA百科taContext = 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}"
          ...>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜