我正在尝试从datagridview创建柱形图,该视图从数据库下载DataSet。是datagridview2。我在公共类中编写了void loaddata()。至于Chart是演唱会,我是由chart()public void类编写的。我要显示:
XvalueMember : ORDER_NUMBER
YvalueMember: TOTAL_TIME
有我的代码:
GenerateCharts.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Windows.Forms.DataVisualization.Charting;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Text.RegularExpressions;
namespace ControlBase
{
public partial class GenerateCharts : Form
{
public GenerateCharts()
{
InitializeComponent();
}
public void loaddata()
{
try
{
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
MySqlDataAdapter adapter2 = new MySqlDataAdapter("SELECT w.FNAME, w.LNAME, z.ORDER_DESC AS 'ORDER DESCRIPTION', o.ORDER_NUMBER AS 'ORDER NUMBER', SEC_TO_TIME(SUM(TIME_TO_SEC(s.BEGIN_DATE) - TIME_TO_SEC(s.END_DATE))) AS 'TOTAL TIME OF ORDER' FROM projekt1.status_order s INNER JOIN projekt1.workers p ON s.ID_WORKER = w.ID_WORKER INNER JOIN projekt1.orders z ON s.ID_ORDER = o.ID_ORDER WHERE p.ID_WORKER ='" + int.Parse(textBox1.Text)+ "' AND BEGIN_DATE >= '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' AND END_DATE <= '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "' GROUP BY s.ID_ORDER;", connection);
connection.Open();
DataSet ds2 = new DataSet();
adapter2.Fill(ds2, "status_order");
dataGridView2.DataSource = ds2.Tables["status_order"];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void chart()
{
MySqlConnection connection = new MySqlConnection();
connection.ConnectionString = "datasource=localhost;port=3306;username=root;password=";
connection.Open();
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT w.FNAME, w.LNAME, z.ORDER_DESC AS 'ORDER DESCRIPTION', o.ORDER_NUMBER AS 'ORDER NUMBER', SEC_TO_TIME(SUM(TIME_TO_SEC(s.BEGIN_DATE) - TIME_TO_SEC(s.END_DATE))) AS 'TOTAL_TIME' FROM projekt1.status_order s INNER JOIN projekt1.workers p ON s.ID_WORKER = w.ID_WORKER INNER JOIN projekt1.orders z ON s.ID_ORDER = o.ID_ORDER WHERE p.ID_WORKER ='" + int.Parse(textBox1.Text)+ "' AND BEGIN_DATE >= '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' AND END_DATE <= '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "' GROUP BY s.ID_ORDER;";
MySqlDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
chart1.Series[0].Points.AddXY(reader.GetString("ORDER_NUMBER"), reader.GetDateTime("TOTAL_TIME"));
}
}
private void button1_Click(object sender, EventArgs e)
{
chart1.Series.Clear();
chart();
loaddata();
}
}
}
启动此应用并尝试生成列后,我出现错误:
System.ArgumentOutOfRangeException: "The index was out of range. It must have a non-negative value and less than the size of the collection. Parameter name: index "
我也尝试过从datagridview2获取数据,但是它没有用。有人可以解释如何更改该代码吗?寻求帮助。
答案 0 :(得分:1)
您调用chart1.Series.Clear();
,然后在chart()
方法中尝试索引到集合:chart1.Series[0]
。您需要先创建一个新的Series
并添加它,然后才能对其进行索引。