0

How do I make the chart run by connecting to the Datagrid view?

well, I want the chart to run once every 10 seconds. and charts take data from dtgrid view.

For example: in 10 seconds, 10 data appear in the grid, now the chart also shows a lot of data in the grid view. so, even in the next 10 seconds if in dtgridview appears 15 data, then the chart shows how much data entered into dtgridview.

but i dont know how to make code like that.

you can make dummy data. because I also don't know where I will put the dummy data. LMAO :"(

I run the chart every 10 seconds. but it's still without data:

the code are ike this:

private void button17_Click(object sender, EventArgs e)
        {
            kayChart cpuData = new kayChart(chart1, 60);
            cpuData.serieName = "gsm";

            Task.Factory.StartNew(() =>
            {
                cpuData.updateChart(updateWithCPU, 10000);
            });
        }

so please help me

sorry my english is broke. im from indonesia btw thanks

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
fayyy
  • 1
  • I suggest use SignalR or Threading.timer for update every 10 second – Fadly Nov 14 '19 at 04:08
  • I am confused as to why you need a timer? If the chart is “properly” data bound to the grid, it should “update” the chart when the grid changes. I do not see where a “timer” is needed. – JohnG Nov 14 '19 at 05:28
  • @JohnG What OP want to is a timed refresh, not real-time refresh. – 大陸北方網友 Nov 14 '19 at 06:05
  • @JohnG he want the chart is updated once every 10 seconds, i don't know what he try to achieve – Fadly Nov 14 '19 at 06:27

2 Answers2

0

You can try this, use timer component to trigger a function/procedure to update UI component in every 10 seconds.

private void UpdateUI()
{
    loaddata();
    chart1.Update();
    chart1.Refresh();
}

private void loaddata()
{
    //pull datas from datagrid to chart
}

private void InitializeTimer()  
{  
   // Run this procedure in an appropriate event (every 10 seconds).  
   timer1.Interval = 10000;  // in miliseconds
   timer1.Enabled = true;  
   // Hook up timer's tick event handler.  
   this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  
}  

private void timer1_Tick(object sender, System.EventArgs e)     
{  
   UpdateUI();
}  

reference:

Fadly
  • 191
  • 10
0

Maybe you can achieve a timed refresh by using Timer. The following is the code you can refer to.

public Form1()
{
    InitializeComponent();

    // Set interval 10s
    this.myTimer = new System.Timers.Timer(10000);
    this.myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
    this.myTimer.AutoReset = true;
    this.myTimer.Enabled = true;
    this.myTimer.Start();

    chart1.Titles.Add("Line Chart");
    chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;
    chart1.ChartAreas[0].Axes[1].MajorGrid.Enabled = false;

    myTimer.SynchronizingObject = this;
}

// Define a timer
private System.Timers.Timer myTimer;

private void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Call methond Refresh
    ChartRefresh();
    Console.WriteLine("refresh");
}

private void btLoadRawDate_Click(object sender, EventArgs e)
{
    // Set the original data
    DataTable dt = new DataTable("cart");
    DataColumn dc1 = new DataColumn("name", Type.GetType("System.String"));
    DataColumn dc2 = new DataColumn("price", Type.GetType("System.Int16"));
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
    for (int i = 0; i < 10; i++)
    {
        DataRow dr = dt.NewRow();
        dr["name"] = i + 1;
        dr["price"] = i * 10;
        dt.Rows.Add(dr);
    }
    dataGridView1.DataSource = dt;
}

private void ChartRefresh()
{
    // Traverse the DataGridView to get the data
    List<string> brand = new List<string>();
    List<int> price = new List<int>();
    for (int row = 0; row < dataGridView1.Rows.Count - 1; row++)
    {
        brand.Add(dataGridView1.Rows[row].Cells[0].Value == null ? "" : dataGridView1.Rows[row].Cells[0].Value.ToString());
        price.Add(dataGridView1.Rows[row].Cells[1].Value == null ? 0 : Convert.ToInt32(dataGridView1.Rows[row].Cells[1].Value));
    }
    chart1.Series.Clear();
    chart1.Series.Add("Price");
    chart1.Series["Price"].Points.DataBindXY(brand, brand);
    chart1.Series[0].ChartType = SeriesChartType.Column;
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37