图表数据绑定到数据表 - 表不更新 [英] Chart DataBinding to DataTable - Chart Not Updating

查看:316
本文介绍了图表数据绑定到数据表 - 表不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图数据绑定一个数据表。我想,因为它们被添加的图表来显示新的行,但是,当新的行被添加到表中的图表没有更新。我已经验证这两个 tableDataSource 包含新行,但 chart.Series [ TestTable的。Points.Count 从不 5 改变。

样code,基于问题无法绑定数据表到图表控制时,如下所示。我要么想知道是否有错误或遗漏与下面的code或不同的,更好的方法,达到同样的目标。我知道如何手动添加指向系列,但我想看看如何做到这一点使用数据绑定。

 随机R =新的随机();
定时器定时器=新的Timer();
数据表表=新的DataTable(TestTable的);
日期时间日期=新的日期时间(2013年1,1);
IList的tableDataSource = NULL;无效timer_Tick(对象发件人,EventArgs的发送)
{
    table.Rows.Add(日期,r.NextDouble());
    日期= date.AddDays(1);    chart.Update();
}无效MainForm_Load(对象发件人,EventArgs的发送)
{
    table.Columns.Add(日期的typeof(DateTime的));
    table.Columns.Add(百分比中的typeof(双));    的for(int i = 0;我小于5;我++)
    {
        table.Rows.Add(日期,r.NextDouble());
        日期= date.AddDays(1);
    }    tableDataSource =(表作为IListSource).GetList();
    chart.DataBindTable(tableDataSource,日期);    timer.Interval = 500;
    timer.Tick + =新的EventHandler(timer_Tick);
    timer.Start();
}


解决方案

尝试使用表作为数据源,而不是:

  // tableDataSource =(表作为IListSource).GetList();
// chart.DataBindTable(tableDataSource,日期);chart.Series.Add(测试);
chart.Series [测试] = XValueMember日期。
chart.Series [测试] = YValueMembers百分比。
chart.DataSource =表;
chart.DataBind();

然后在Tick事件,再次调用DataBind,而不是更新:

 无效timer_Tick(对象发件人,EventArgs的发送){
  table.Rows.Add(日期,r.NextDouble());
  日期= date.AddDays(1);  //chart.Update();
  chart.DataBind();
}

I'm attempting to databind a Chart to a DataTable. I would like the chart to display new rows as they are added, but the chart is not updating when new rows are added to the table. I have verified that both table and tableDataSource contain the new rows, but chart.Series["TestTable"].Points.Count never changes from 5.

Sample code, based on the question Can't bind datatable to Chart Control, is shown below. I would either like to know if there is an error or omission with the code below, or a different, better approach that achieves the same objective. I know how to manually add points to a Series, but I would like to see how to do this using data binding.

Random r = new Random();
Timer timer = new Timer();
DataTable table = new DataTable("TestTable");
DateTime date = new DateTime(2013, 1, 1);
IList tableDataSource = null;

void timer_Tick(object sender, EventArgs e)
{
    table.Rows.Add(date, r.NextDouble());
    date = date.AddDays(1);

    chart.Update();
}

void MainForm_Load(object sender, EventArgs e)
{
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("Percent", typeof(double));

    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(date, r.NextDouble());
        date = date.AddDays(1);
    }

    tableDataSource = (table as IListSource).GetList();
    chart.DataBindTable(tableDataSource, "Date");

    timer.Interval = 500;
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

解决方案

Try using the table as a DataSource instead:

// tableDataSource = (table as IListSource).GetList();
// chart.DataBindTable(tableDataSource, "Date");

chart.Series.Add("test");
chart.Series["test"].XValueMember = "Date";
chart.Series["test"].YValueMembers = "Percent";
chart.DataSource = table;
chart.DataBind();

and then in the tick event, call DataBind again instead of the Update:

void timer_Tick(object sender, EventArgs e) {
  table.Rows.Add(date, r.NextDouble());
  date = date.AddDays(1);

  //chart.Update();
  chart.DataBind();
}

这篇关于图表数据绑定到数据表 - 表不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆