Home > DeveloperSection > Forums > how do you bind datagridview with chart?
Ho Hui Jun
Ho Hui Jun

Total Post:5

Points:35
Posted on    December-29-2015 11:26 AM

 C# Chart  Datagridview 
Ratings:


 2 Reply(s)
 446  View(s)
Rate this:
hi there, I really need help on this project that I am dealing with. It is to calculate free surface effect of a vessel on large angle which I have no idea where it when wrong.

it doesn't show any chart on the right side. 
below is the code for the chart. I do appreciate if anyone could help me with this.

Private Sub GZChart_Click(sender As Object, e As EventArgs) Handles GZChart.Click

        GZChart.ChartAreas.Add("area")
        GZChart.ChartAreas("area").AxisX.Minimum = 0
        GZChart.ChartAreas("area").AxisY.Minimum = 0
        GZChart.ChartAreas("area").AxisX.Maximum = 100
        GZChart.ChartAreas("area").AxisY.Maximum = 20
        GZChart.ChartAreas("area").AxisX.Interval = 10
        GZChart.ChartAreas("area").AxisX.Interval = 0.5

        GZChart.Series("Series1").Color = Color.Red
        GZChart.Series("Series1").ChartType = DataVisualization.Charting.SeriesChartType.SplineArea

        GZChart.Series.Clear()
        GZChart.Series.Add("Series1")

        GZChart.Series("Series1").Points.Clear()
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(0).Cells(1).Value, DataGridView.Rows(0).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(1).Cells(1).Value, DataGridView.Rows(1).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(2).Cells(1).Value, DataGridView.Rows(2).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(3).Cells(1).Value, DataGridView.Rows(3).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(4).Cells(1).Value, DataGridView.Rows(4).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(5).Cells(1).Value, DataGridView.Rows(5).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(6).Cells(1).Value, DataGridView.Rows(6).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(7).Cells(1).Value, DataGridView.Rows(7).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(8).Cells(1).Value, DataGridView.Rows(8).Cells(3).Value)
        GZChart.Series("Series1").Points.AddXY(DataGridView.Columns(3).HeaderText & "--", DataGridView.Rows(9).Cells(1).Value, DataGridView.Rows(9).Cells(3).Value)

    End Sub

  


Pawan Shukla

Total Post:37

Points:267
Posted on    January-04-2016 3:06 AM

Hi.

Step -1 
Craete a class

        public class Product
        {
            public string ProductName { get; set; }
            public int Qty { get; set; }
            public float Price { get; set; }
        }

Step-2
Add dummy record in list and bind to datagridview

            List<Product> list = new List<Product>();
            list.Add(new Product { ProductName = "Pendrive", Qty = 10, Price = 323 });
            list.Add(new Product { ProductName = "Laptop", Qty = 23, Price = 1445 });
            list.Add(new Product { ProductName = "Computer", Qty = 34, Price = 1446 });
            DGV1.DataSource = list;

Step-3
Add Series to chart and bind data from datagridview

            chart1.Series.Clear();
            chart1.Series.Add("Pendrive");
            chart1.Series.Add("Laptop");
            chart1.Series.Add("Computer");
            chart1.Series["Pendrive"].Points.Clear();
            chart1.Series["Pendrive"].Points.AddXY("Product", DGV1.Rows[0].Cells[1].Value);
            chart1.Series["Laptop"].Points.Clear();
            chart1.Series["Laptop"].Points.AddXY("Product", DGV1.Rows[1].Cells[1].Value);
            chart1.Series["Computer"].Points.Clear();
            chart1.Series["Computer"].Points.AddXY("Product", DGV1.Rows[2].Cells[1].Value);
Output:



Ho Hui Jun
Ho Hui Jun

Total Post:5

Points:35
Posted on    January-08-2016 12:16 AM

Hi, Pawan Shukla

Thank you so much , it helps alot.

Don't want to miss updates? Please click the below button!

Follow MindStick