I create the columns of my Silverlight DataGrid dynamically in the code behind using a List as follows:
private void BuildQuotationDGColumns(List<ProductCategory> ProdCatList)
{
// add a template column with drop down list of products
DataGridTemplateColumn ProductColumn = new DataGridTemplateColumn();
ProductColumn.Header="Choose Product";
ProductColumn.CellTemplate=(DataTemplate) Resources["ProductDDLTemplate"];
QuotationDG.Columns.Add(ProductColumn);
// for each entity in ProdCatList add a text column
foreach (ProductCategory ProdCat in ProdCatList)
{
DataGridTemplateColumn ProdCatColumn = new DataGridTemplateColumn();
ProdCatColumn.Header = ProdCat.Name;
ProdCatColumn.CellTemplate = (DataTemplate)Resources["MoneyTemplate"];
ProdCatColumn.CellEditingTemplate = (DataTemplate)Resources["MoneyEditingTemplate"];
QuotationDG.Columns.Add(ProdCatColumn);
}
insertDummyRow(ProdCatList);
}
I need to insert a blank row in my Datagrid using insertDummyRow. Since the columns are dynamic and are only known in runtime, I need to create an entity whose attributes can be set in run time.
I thought of converting my ProdCatList into a Class, so that instances of this class would form the grid rows, but I couldn't understand how to do this conversion
EDIT:
Based on Bahman_Aries's solution below, I'm trying to add data into my row but I'm getting empty row and my data (column.Header.ToString()) isn't added. Here's my code:
CultureInfo provider= new CultureInfo("en-US");
Object[] myrow= new Object[QuotationDG.Columns.Count];
int i=0;
foreach(DataGridColumn column in QuotationDG.Columns)
{
myrow[i] = Convert.ChangeType(column.Header.ToString(), typeof(object), provider);
i++;
}
MyData.Add(myrow);
QuotationDG.ItemsSource = MyData;
Can you point me on what i'm doing wrong?
This is the implementation for the Grid Templates:
<UserControl.Resources>
<DataTemplate x:Key="MoneyTemplate">
<TextBlock></TextBlock>
</DataTemplate>
<DataTemplate x:Key="MoneyEditingTemplate">
<TextBlock></TextBlock>
</DataTemplate>
<DataTemplate x:Key="ProductDDLTemplate">
<ComboBox />
</DataTemplate>
</UserControl.Resources>
Okey, let me clear up some confusion before we proceed:
Not necessarily, you can use a list of
objects
.There is no need to create a complex data structure just to insert a blank row into the
DataGrid
, something as simple asQuotationDG.Items.Add("");
will do.So from what I understand, either you're overcomplicating the issue or you did not provide enough information and I'm oversimplifying it! (If so please let me know so I can correct this straight away).
Anyhow, Since you defined a
CellEditingTemplate
I assume your grid cells are editable, therefore you can not useQuotationDG.Items.Add
because it'll prevent editing. Instead you should define a list, add your data to it and then use it asItemsSource
of yourDataGrid
:Edit:
Since there is no data-binding in your
CellTemplates
, nothing can be seen in yourDataGrid
. For this to work, when columns are creating dynamically you need to add correspondingDataBindings
as well:To see if UI shows any data, you can try my modified version of your add-data procedure:
To be sure that data entered by user is going to update the source (e.g.
MyData
) as well, set a break-point and checkMyData
for any changes after editing data-grid cells manually.