In your XAML bind your datagrid to a ObservableCollection of objects of a certain class that has properties.
XAML:
<WpfToolkit:DataGrid
x:Name="MyDataGrid"
ItemsSource="{Binding Path=Collection}"
HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="False"
RowHeaderWidth="25" RowHeight="25"/>
Next you can create your columns programmatically in C#/VBA and bind each individual column to a property of the class to which the ObservableCollection contains objects of it. By adding objects of the class you will be adding rows to the datagrid. In other words, each object of the class in the ObservableCollection will be a row and the properties of the class will be the columns.
Here is an example to how you can bind your columns programmatically...
C#:
ObservableCollection<IData> datagridData = new ObservableCollection< IData >();
Binding items = new Binding();
PropertyPath path = new PropertyPath("Name"); // 'Name' is actually the name of the variable representing the property
items.Path = path;
MyDataGrid.Columns.Add(new DataGridTextColumn()
{
Header = "Names",
Width = 275,
Binding = items
});
//repeat the creation of columns
//...
//- Add some objects to the ObservableCollection
//- Then bind the ItemsSource of the datagrid to the ObservableCollection
datagridData .Add(new Data("Bob", string.Empty));
MyDataGrid.DataContext = new DataModel{ MyData = datagridData };
*Edit:
Sorry about that! Here is how you can achieve the same thing entirely in XAML:
<WpfToolkit:DataGrid
x:Name="MyDataGrid"
ItemsSource="{Binding Path=Collection}"
HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeRows="False" CanUserSortColumns="False"
AutoGenerateColumns="False"
RowHeaderWidth="25" RowHeight="25">
<WpfToolkit:DataGridTextColumn
Header="Names" Width="2*"
Binding="{Binding Path=Name}"/>
<WpfToolkit:DataGridTextColumn
Header="Names" Width="2*"
Binding="{Binding Path=Age}"/>
</WpfToolkit:DataGrid.Columns>
</WpfToolkit:DataGrid>
Edit 2: Here is what the code of the ObservableCollection and class may look like in C#:
public class DataModel
{
public ObservableCollection<IData> MyData{ get; set; }
}
public interface IData
{
string Name{ get; set; }
string Age{ get; set; }
}
public class Data : IData
{
public Data(string name, string age)
{
Name= name;
Age= age;
}
public string Name{ get; set; }
public string Age{ get; set; }
}