【问题标题】:Databound MSChart not updating数据绑定 MSChart 未更新
【发布时间】:2020-12-02 19:58:36
【问题描述】:

我有一个定期更新数据表的应用程序,我将该数据表绑定到图表作为数据源。

    private DataTable seriesData = new DataTable();

    //in load handler
    seriesData.Columns.Add("TagName", typeof(string));
    seriesData.Columns.Add("XValue", typeof(DateTime));
    seriesData.Columns.Add("YValue", typeof(double));
    chart.DataSource = seriesData;
    chart.DataBindCrossTable(seriesData.DefaultView, "TagName", "XValue", "YValue", "");

然后数据源以指定的间隔填充数据

    var newRow = seriesData.NewRow();
    newRow["XValue"] = ts;
    newRow["YValue"] = Convert.ToDouble(tag.Value);
    newRow["TagName"] = tag.Name;
    //add new row to dataset
    seriesData.Rows.Add(newRow);

问题是,当使用新数据更新数据源时,图表不会自动更新。 我认为需要使用chart.DataBind(); 刷新它,但这并没有刷新图表。我也尝试过使用chart.Invalidate();,但这也没有用。我确实验证了数据表中有数据,并且它在附加到图表的数据视图中发生了变化。

有什么想法吗?

谢谢

【问题讨论】:

    标签: c# winforms mschart


    【解决方案1】:

    经过大量搜索,我找到了答案。所以每次我想更新图表时,我都必须清除系列集合并再次调用 DataBindCrossTable。

        chart.Series.Clear();
        chart.DataBindCrossTable(seriesData.DefaultView, "TagName", "XValue", "YValue", "");
    

    尽管每次需要进行更新时这似乎都是一项昂贵的操作,但我看不到其他更新绑定图表的方法。

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 1970-01-01
      • 2012-11-07
      • 2010-10-14
      • 1970-01-01
      • 2018-10-26
      • 2012-07-27
      • 2016-10-28
      • 1970-01-01
      相关资源
      最近更新 更多