0

Ok, so the documentation for ModernUI Charts is dreadfully lacking especially in very common things such as binding to datatables and therefore SQL tables. There's one question on the codeplex site's discussion where someone asks a similar question but a flippant reply about "viewing the source code would give you your answer" was the only reply.

I know I'm close, I'm trying to bind a bar or column chart to a column of rows in a SQL table in C#. So nothing at all complex. I'm using TableAdapters to get data in and out of SQL so really the question is about binding datatables or converting a datatable into a collection to be parsed by the Chart.

Any help or examples someone can provide?

Thanks!

1 Answers1

0

If you follow the 'How-To' given by the developer, they use an ObservableCollection to bind to the Chart. If you can accept this extra layer of data, you can copy all of your DataTable rows into an ObservableCollection and bind it to the chart.

private void ConvertToObservableCollection(DataTable dataTable)
{
    var myCollection = new ObservableCollection<MyObject>();
    var myDataView = new DataView(dataTable);

    foreach (DataRowView row in myDataView)
    {
        var obj = new MyObject()
        {
            id = row["id"].ToString(),
            name = row["name"].ToString()
        };
        myCollection.Add(obj);
    }
}

Remember to copy over all of the require properties to your collection. Bear in mind that changes to the collection are not reflected automatically in the DataTable; you'd have to code for this.

Alex Hopkins
  • 872
  • 9
  • 11