HOW DO YOU BIND DATAGRIDVIEW WITH CHART?

Ho Hui Jun

Total Post:5

Points:35
Posted by  Ho Hui Jun
C#
 861  View(s)
Ratings:
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

  
  1. Pawan Shukla

    Post:38

    Points:274
    Re: how do you bind datagridview with chart?

    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:


  1. Ho Hui Jun

    Post:5

    Points:35
    Re: how do you bind datagridview with chart?

    Hi, Pawan Shukla

    Thank you so much , it helps alot.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!