How to Bind a DataGrid to a DataTable all in code behind?

后端 未结 2 1559
醉梦人生
醉梦人生 2021-01-15 13:33

How to bind a DataGrid to a DataTable in C# code behind? (All controls are generated in run-time, so no XAML please)

I tried Binding(), set the DataContext, set the

相关标签:
2条回答
  • 2021-01-15 14:20

    Try binding to one of the Tables in your dataset:

    datagrid.ItemsSource = dataset.Tables[0].DefaultView;
    
    0 讨论(0)
  • 2021-01-15 14:37

    Also, to keep a good abstraction layer between your data and its representation on your forms, I would suggest you to set the property AutoGenerateColumns of your DataGridView to false and add just the columns you want displayed by hand, with the header name of your choice (in this case, do not forget to set the columns' DataPropertyName property).

    0 讨论(0)
提交回复
热议问题