J.Memisevic
J.Memisevic

Reputation: 1454

Databinding DataTable to DataGrid - Code Behind

I have created DataGrid and I wan't to add DataTemplateColumns generic , so it depends on number of columns that Data table ,that I put as source , have! But when i do all of that and bind DataGrid columns to DataTable columns i have scenario where i got right number of row in DataGrid , but it's data from first row only. So , what am I doing wrong?

Here is the code :

Binding binding = new Binding();
binding.Path = new 
PropertyPath(dataTable.Columns[i].ColumnName.ToString());
binding.Source = dataTable

FrameworkElementFactory textBlock = new 
FrameworkElementFactory(typeof(TextBlock));
textBlock.SetValue(TextBlock.TextProperty, binding);

DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = textBlock;
dataGridTemplateColumn.CellTemplate = dataTemplate;
dgTab1.Columns.Add(dataGridTemplateColumn);

I think i have a problem with binding, but i don't know how to fix it, obviously!

Upvotes: 0

Views: 44

Answers (1)

mm8
mm8

Reputation: 169360

Use the SetBinding (instead of SetValue) method to bind to the Text property:

FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
textBlock.SetBinding(TextBlock.TextProperty, binding);

Upvotes: 1

Related Questions