Create WPF DataGrid from code-behind

Jun 15, 2009 at 1:21 PM

Hello,

I would like to create a WPF Datagrid entirely from code behind (C#). How can I do this ?

Jun 16, 2009 at 2:04 AM
Edited Jun 16, 2009 at 2:05 AM

I want also! but I found DatagridColume can not to create new!

help!

Thanks!

Jun 16, 2009 at 8:18 AM

 

 

Something like this will work:

DataGrid dataGrid1 = new DataGrid();

dataGrid1.AutoGenerateColumns = false;

dataGrid1.Columns.Add(new DataGridTextColumn() { Binding = new Binding("aaa"), Header = "Label" });

dataGrid1.Columns.Add(new DataGridTextColumn() { Binding = new Binding("bbb"), Header = "Count" });

dataGrid1.Columns.Add(new DataGridGidColumn() { Binding = new Binding("ccc"), Header = "Frequency" });

dataGrid1.ItemsSource = dataSource;


 

 

Jun 16, 2009 at 9:29 AM

thanks wintal, what I am missing is the different "using" statement needed.

 

 

Jul 7, 2009 at 4:58 PM

I had the same problem.

The using statement needed for the WPF DataGrid is:

using Microsoft.Windows.Controls;