0

我正在尝试从从数据库下载 DataSet 的 datagridview 创建柱形图。是datagridview 2。我在公共类void loaddata()中写过。至于chart是我由chart() public void class写的音乐会。我要显示:

XvalueMember : ORDER_NUMBER
YvalueMember: TOTAL_TIME

有我的代码:

生成图表.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 获取数据,但没有奏效。有人可以解释如何更改该代码吗?感谢您的帮助。

4

1 回答 1

1

你调用chart1.Series.Clear();,然后在chart()方法中尝试索引到集合中:chart1.Series[0]。您需要创建一个新的Series并添加它,然后才能对其进行索引。

于 2019-03-06T20:02:39.560 回答