Vigna
Vigna

Reputation: 113

Bind data from datagridview with chart in C#

I'm creating one application, which use to determine the raw data and those raw data values are imported in datagridview. Now I need to produce that raw data values in graphical representation. I have datgridview with multiple columns and I want to plot graph/chart with datagridview column values in C#.

This is what I tried.

    private void button1_Click(object sender, EventArgs e)
    {
        
        try
        {
            chart1.Visible = true;
            this.chart1.Series["0"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE1");
            this.chart1.Series["1"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE2");
            this.chart1.Series["2"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE3");
            this.chart1.Series["3"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE4");
            this.chart1.Series["4"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE5");
            this.chart1.Series["5"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE6");
            this.chart1.Series["6"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE7");
            this.chart1.Series["7"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE8");
            this.chart1.Series["8"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE9");
            this.chart1.Series["9"].Points.DataBindY((DataView)dataGridView1.DataSource, "SAMPLE10");

        }
        catch
        {
        }
    }

But no use. Is there any other way or method to plot the chart using datagridview column values directly?

Thanks in advance.

Upvotes: 1

Views: 3039

Answers (1)

Feroz Khan
Feroz Khan

Reputation: 15

Use below code for chart through DataGridview

foreach (DataGridViewRow row in datagridview.Rows)
            {

                //chartBpComplaince.Series.Clear();
                Series S = chartBpComplaince.Series.Add(row.Cells[2].Value.ToString());

                 S.Points.AddXY(row.Cells[4].Value.ToString(), row.Cells[3].Value.ToString());
                 S.ChartType = SeriesChartType.Column;
                 S.IsValueShownAsLabel = true;
}

Upvotes: 1

Related Questions