Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
            }
Posted
Updated 9-Jan-18 3:57am
Comments
Karthik_Mahalingam 9-Jan-18 9:45am    
which two columns?
MaddoxSt 9-Jan-18 9:51am    
the category and the total from the billinggridview i have a pic of it above
Maciej Los 9-Jan-18 11:14am    
Does your DataGridView is bound with datasource? If yes, you have to use that source instead of reding data from DataGridViewRow.

1 solution

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;
 
Share this answer
 
v2
Comments
MaddoxSt 9-Jan-18 23:02pm    
Kathik Bangalore bro it throws an exception of
A chart element with the name 'PieChart' could not be found in the 'SeriesCollection'
Karthik_Mahalingam 9-Jan-18 23:11pm    
You will have to use your chart series name
Karthik_Mahalingam 9-Jan-18 23:56pm    
replace "PieChart" with "Pie"
MaddoxSt 10-Jan-18 0:12am    
i checked it the name of the series was Series1 but when i changed it and press the button nothing happens
Karthik_Mahalingam 10-Jan-18 0:23am    
try
DataTable dtPieChartData = new DataTable();
            dtPieChartData.Columns.Add("Category");
            dtPieChartData.Columns.Add("Total");
            dtPieChartData.Rows.Add("Category 1", 100);
            dtPieChartData.Rows.Add("Category 2", 200);
            dtPieChartData.Rows.Add("Category 3", 300); 
            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;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900