Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Team,

I am trying to create a chart based on my datagrid. My datagrid contains bound and unbound columns, i have succesfully been apple to display the bound columns in the chart and unfortunately not been able to do the same for the unbound columns.

i have read How to create a pie chart from a datagridview for specific columns[^]

to see if i can assign to my chart specific columns to display. But unfortunaly not benn able to do so.

any suggestion on how display both bound and unbound datagrid columns to chart?

What I have tried:

I did try to fetch my gridview data and save to datatable like seen below:

private void chdata()
{
DataTable dt = new DataTable();
foreach (GridColumn column in ProdgridView.VisibleColumns)
{
dt.Columns.Add(column.FieldName, column.ColumnType);
}
for (int i = 0; i < ProdgridView.DataRowCount; i++)
{
DataRow row = dt.NewRow();
foreach (GridColumn column in ProdgridView.VisibleColumns)
{
row[column.FieldName] = ProdgridView.GetRowCellValue(i, column);
}
dt.Rows.Add(row);

ProdchartControl.DataSource = dt;
ProdchartControl.Series["ProdA"].ArgumentDataMember = "Prod_Date";
ProdchartControl.Series["ProdA"].ValueDataMembers.AddRange("AProd");
}
}

but i get the error: System.NotSupportedException: 'DataSet does not support System.Nullable<>.' when trying to use the converted datagridview to Datatable as my chart datasource.

Any suggestion would be appreciated.

Regards.
Posted
Updated 26-Sep-19 23:17pm
v3

1 solution

As per documentation[^] chart control accepts only one datasource type of datatable, dataview, etc.
So, if you want to bind data to a chart object, you have to re-write bounded and unbounded data from datagridview to any of object which is acceptable as a chart datasource (for example: datatable).
 
Share this answer
 
Comments
PmJN 24-Sep-19 7:24am    
Hi Maciej,

Thanks for your input. I tried to convert my datagridview data to Datatable using but still can not achieve what i want.

here is how i tried to convert my datagridView data to a Datatable.

private void chdata()
{
DataTable dt = new DataTable();
foreach (GridColumn column in ProdgridView.VisibleColumns)
{
dt.Columns.Add(column.FieldName, column.ColumnType);
}
for (int i = 0; i < ProdgridView.DataRowCount; i++)
{
DataRow row = dt.NewRow();
foreach (GridColumn column in ProdgridView.VisibleColumns)
{
row[column.FieldName] = ProdgridView.GetRowCellValue(i, column);
}
dt.Rows.Add(row);

ProdchartControl.DataSource = dt;
ProdchartControl.Series["ProdA"].ArgumentDataMember = "Prod_Date";
ProdchartControl.Series["ProdA"].ValueDataMembers.AddRange("AProd");
}
}

but i get the error: System.NotSupportedException: 'DataSet does not support System.Nullable<>.' when trying to use the converted datagridview to Datatable as my chart datasource.

Any suggestion would be appreciated.

Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900