如何从datagridview为特定列创建饼图 [英] How to create a pie chart from a datagridview for specific columns

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

问题描述

我有一个5列的datagridview,我想在按下按钮时制作一个饼图,只需要两列datagridview

这里是datagridview的图片



datagridview的图片 [ ^ ]

所以从这个datagridview我想创建一个饼图类别和总和我可以得到请帮助:)



我尝试过:



I have a datagridview with 5 columns and i want to make a pie chart on the press of button by taking only two columns of a datagridview
here is the picture of the datagridview

Image of the datagridview[^]
So from this datagridview i want to create the pie chart for category and total can i get help please :)

What I have tried:

private void generateChart_Click(object sender, EventArgs e)
        {
            Series s = pieChart.Series.Add("pie");
            s.ChartType = SeriesChartType.Pie;
            s.IsValueShownAsLabel = true;
            for (int colindex = 1; colindex < billingGridView.Columns.Count; colindex++)
            {
                s.Points.AddXY(billingGridView.Columns[colindex].HeaderText,
                               billingGridView[colindex, 0].Value);
            }

推荐答案

更新解决方案



Updated Solution

DataTable dtPieChartData = new DataTable();
          dtPieChartData.Columns.Add("Category");
          dtPieChartData.Columns.Add("Total");
          foreach (DataGridViewRow row in billingGridView.Rows)
          {
              int indexCategoryColumn = 1; // take care of index
              int indexTotalColumn = 4; // take care of index
              dtPieChartData.Rows.Add(row.Cells[indexCategoryColumn].Value,row.Cells[indexTotalColumn].Value);
          }

            chart1.DataSource = dtPieChartData;
           chart1.Series["Series1"].XValueMember = "Category";
           chart1.Series["Series1"].YValueMembers = "Total";
           this.chart1.Titles.Add("Category Title");
           chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
           chart1.Series["Series1"].IsValueShownAsLabel = true;


这篇关于如何从datagridview为特定列创建饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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