我正在尝试通过其NumericTimeSeriesDataAppearance实例将DataView绑定到NumericTimeSeries,但系列最终得到零点。我正在使用以下代码进行测试:
DataTable table = new DataTable();
table.Columns.Add("date", typeof(DateTime));
table.Columns.Add("value", typeof(double));
DataView view = new DataView(table);
view.AllowDelete = true;
view.AllowNew = true;
view.AllowEdit = true;
NumericTimeSeries series = new NumericTimeSeries();
series.Data.TimeValueColumn = "date";
series.Data.ValueColumn = "value";
series.Data.DataSource = view;
series.DataBind();
// Update series when data is changed
view.ListChanged += (sender, args) => series.DataBind();
table.RowChanged += (sender, args) => series.DataBind();
table.TableNewRow += (sender, args) => series.DataBind();
table.RowDeleted += (sender, args) => series.DataBind();
table.Rows.Add(DateTime.Now, 42.0);
System.Console.WriteLine("Number of datapoints in view is {0}", view.Count);
System.Console.WriteLine("Number of datapoints in series is {0}", series.Points.Count);
这为视图输出1个数据点,但系列输出0。直接绑定到DataTable工作正常,但不足以解决我的问题(我在日期使用DataView对DataTable进行排序)。
答案 0 :(得分:0)
我最终使用触发事件DataTable.RowChanged,TableNewRow和RowDeleted更新NumericTimeSeries:
public void BindSortedViewToSeries(DataTable unsortedTable, DataView sortedView, NumericTimeSeries series)
{
unsortedTable.RowChanged += (sender, args) => MirrorDataView(sortedView, series);
unsortedTable.TableNewRow += (sender, args) => MirrorDataView(sortedView, series);
unsortedTable.RowDeleted += (sender, args) => MirrorDataView(sortedView, series);
MirrorDataView(view, series);
}
private static void MirrorDataView(DataView view, NumericTimeSeries series)
{
series.Data.DataSource = view.ToTable();
series.DataBind();
}