问题
Good day,
i have a little problem. I'm trying to make chart from dataGridView so i wrote short code.
private void create_graphs_Click(object sender, EventArgs e) {
Chart chart1 = new Chart();
chart1.Series[0] = new Series();
chart1.Series[0].XValueMember = dataGridView1.Columns[0].DataPropertyName;
chart1.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
chart1.DataSource = dataGridView1.DataSource;
chart1.Series[0].ChartType = SeriesChartType.Line;
chart1.SaveImage("chart.png", ChartImageFormat.Png);
}
Syntax without error. Problem is, when i start it, it says:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Any suggestion? Thanks in advance.
EDIT 8.7.2017 18:07:
So thanks for your help. I eddited a code and now looks like this:
ChartArea chartArea1 = new ChartArea();
chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chart1.ChartAreas.Add(chartArea1);
chart1.Series.Add(new Series());
chart1.Series[0].XValueMember = dataGridView1.Columns[0].DataPropertyName;
chart1.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
chart1.DataSource = dataGridView1.DataSource;
chart1.Series[0].ChartType = SeriesChartType.Line;
chart1.SaveImage("chart.png", ChartImageFormat.Png);
And its working. Creates a graph but now i need to make that graph, well bigger. This is output image... I don't know how to put image here so at least link: http://imgur.com/a/Y5OaH
Thing is, i have 93 items in my Column so i need that chart to show all 12 months or 13 months... from 1.1.2008 to 1.1.2009
来源:https://stackoverflow.com/questions/45542298/creating-chart-from-datagridview-c-sharp