从DataGridView创建图表 [英] Create chart from DataGridView

查看:60
本文介绍了从DataGridView创建图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 DataGridView 的表单(两列: PartnerName Adult )

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

请,我需要从 DataGridView 填充数据以绘制图表并打印图表

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

推荐答案

您可以从以下代码中获得想法:

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();
}

您可以从用于Microsoft图表控件的示例环境中获取完整的项目代码并从WorkingWithData> DataManipulation> Exporting> DataGridBinding部分中找到代码.

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

这篇关于从DataGridView创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆