Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
195
xamGrid and DataTable of objets or Dictionnary
posted

Hello,

I would like to use binding on a XamGrid with a DataTable or if possible a Dictionnary but I don't know how.

<ig:XamGrid Grid.Row="1"

x:Name="xamGridCategorisations"

RowHover="Row"

AutoGenerateColumns="False" 

InitializeRow="xamGridCategorisations_InitializeRow"

Loaded="xamGridCategorisations_Loaded"

>

</ig:XamGrid>


I want a variable number of columns so I don't have and I don't want any

<ig:XamGrid.Columns> ....</ig:XamGrid.Columns>


The dictionnary that I have is :

public Dictionary<int, List<InverterData>> InverterDatas= new Dictionary<int, List<InverterData>>();

And I want as many column of List<InverterData>

The Header of the column must be the property InverterData's Name (InverterData.Name) which the same for every element of each List<>

And the Cell Value must be value of the Inverter object (InverterData.Value).


The xamGridCategorisations.ItemsSource is assigned with a DataTable which contains lot's of data (for other reasons) and of course the InverterData.

xamGridCategorisations.ItemsSource = _modele.MyDataTable.AsDataView();

DataTable dt = new DataTable();

row = dt.Rows.Add();

row["Category"] = cat;

row["Car"] = car;

row["Product"] = prod;

.....

row["InverterData1"] = id01;

row["InverterData2"] = id02;

....

row["InverterData10"] = id010;


and when the second row is created we have :

row = dt.Rows.Add();

row["Category"] = cat;

row["Car"] = car;

row["Product"] = prod;

.....

row["InverterData1"] = id11;

row["InverterData2"] = id12;

....

row["InverterData10"] = id110;



etc.


So I want to Display and mostly "Bind" only the InverterDataXX (columns and rows) of the DataTable already plug in the Itemsource.

Or if it is not possible then bind the dictionnary



I know how to bind a ObservableCollection to a static column like


<ig:TemplateColumn Key="XX10" HeaderText="Inverter10"  >

<ig:TemplateColumn.ItemTemplate>

<DataTemplate>

<TextBlock DataContext="{Binding MyBindedCollection}" Text="{Binding MyBindedField}" />

</DataTemplate>

</ig:TemplateColumn.ItemTemplate>

</ig:TemplateColumn>

</ig:XamGrid.Columns>


But I don't know how to do it with a variable number of column.

May be Something like ObservableCollection<ObservableCollection>



Please help me

Many thanks


Best Regards,










  • 34690
    Verified Answer
    Offline posted

    Hello,

    I have been investigating into your requirement, although, I'm not entirely sure I completely understand the exact functionality that you are looking for. From what I understand, you do not want to manually generate your columns via the XamGrid.Columns collection, and as such I would recommend setting the AutoGenerateColumns property on your XamGrid to "true," as nothing will be generated from the ItemsSource that you pass to the grid if you are not populating the XamGrid.Columns collection otherwise.

    The XamGrid expects an IEnumerable for its ItemsSource. When in AutoGenerateColumns="True" mode, the XamGrid will look at the items that exist in your collection and use reflection to generate a Column for each property (or column in the case of the DataTable), and display the value of each of the rows. For this reason, if you are looking to bind your DataTable, I believe the "AsDataView()" method that you are using above should work. Otherwise, perhaps you should consider binding to the DefaultView property of your DataTable. As for the Dictionary; Dictionaries are collections<KeyValuePair> and so the columns generated will be for the properties of KeyValuePair if you bind directly to the Dictionary. I would recommend that you bind to the Values collection of your Dictionary instead.

    As for the structure of your data, you have said, "The Header of the column must be the property InverterData's Name (InverterData.Name) which the same for every element of each List<>. And the Cell Value must be value of the Inverter object (InverterData.Value)." Being that it sounds like you are looking to auto-generate your columns, I would recommend continuing with a DataTable for this. If all of your Inverter objects exist in a separate DataTable or a List<Inverter>, you can construct a new DataTable, where each column's name is the name of each of your Inverter objects. You could then populate the Rows collection of your DataTable with the matching values for that named Inverter. Passing this to the XamGrid will give you a Column for each of your Inverter's names, displaying the values for those Inverters, which sounds like the behavior that you are looking to achieve.

    If the above is not exactly the behavior you are looking to achieve on this matter, would it be possible for you to please provide some additional information on the exact functionality you are looking for with the XamGrid?

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer