Create chart from DataGridView

这一生的挚爱 提交于 2020-02-24 10:43:27

问题


I have a form with DataGridView (2 columns: PartnerName, Adult)

Please, I need to fill data from DataGridView to chart and print the chart


回答1:


You can get idea from following code:

private void DataGridBinding_Load(object sender, System.EventArgs e)
{
   // Populate series data using random data
  double[]    yValues = { 23.67, 75.45, 60.45, 34.54, 85.62, 32.43, 55.98, 67.23 };
  for(int pointIndex = 0; pointIndex < yValues.Length; pointIndex++)
  {
    chart1.Series["Series1"].Points.AddXY(1990 + pointIndex, yValues[pointIndex]);
  }

  // Export series values into DataSet object
  dataSet1 = chart1.DataManipulator.ExportSeriesValues("Series1");

  // Data bind DataGrid control. 
  SeriesValuesDataGrid.DataSource = dataSet1;

  // Set Series name for data
  SeriesValuesDataGrid.DataMember = "Series1";

}

private void SeriesValuesDataGrid_CurrentCellChanged(object sender,System.EventArgs e)
{
   // Initializes a new instance of the DataView class
   DataView firstView = new DataView(dataSet1.Tables[0]);

   // Since the DataView implements IEnumerable, pass the reader directly into
   // the DataBind method with the name of the Columns selected in the query    
   chart1.Series["Series1"].Points.DataBindXY(firstView,"X",firstView,"Y");

   // Invalidate Chart
   chart1.Invalidate();
}

You can get complete project code from Samples Environments for Microsoft Chart Controls and find the code from WorkingWithData>DataManipulation>Exporting>DataGridBinding section.



来源:https://stackoverflow.com/questions/26324195/create-chart-from-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!