DateTime and DbNull Values
Posted: Thu Oct 06, 2005 7:25 am
Hi there, everybody. Using the 2.0.2078.14844 build of TeeChart v2 I discovered some more problems with DbNull values.
I managed to get it to display the label for a certain point as stated in a previous thread here. Well, now I discovered that when using DateTime values from a DataTable as XValues for a series and using these as DateTime values (DateTime = true) and if that data contains DbNull values, the dates are corrupted, lost, whatever.
So, in a demo project I am able to demonstrate this very well, the code of which I post below. What happens is that if the dataset contains DbNull values, the dates become 1900-01-01 etc instead of their actual values. In the demo project you can see both behaviours by changing the variable withNulls to true or false between runs.
Is this a known bug or am I just doing something seriously wrong?
Cheers,
Tommie
I managed to get it to display the label for a certain point as stated in a previous thread here. Well, now I discovered that when using DateTime values from a DataTable as XValues for a series and using these as DateTime values (DateTime = true) and if that data contains DbNull values, the dates are corrupted, lost, whatever.
So, in a demo project I am able to demonstrate this very well, the code of which I post below. What happens is that if the dataset contains DbNull values, the dates become 1900-01-01 etc instead of their actual values. In the demo project you can see both behaviours by changing the variable withNulls to true or false between runs.
Is this a known bug or am I just doing something seriously wrong?
Cheers,
Tommie
Code: Select all
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TeeChartLabelProblemDemo
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Steema.TeeChart.TChart tChart1;
private DevExpress.XtraEditors.SimpleButton btnAddSeries;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private bool withNulls = true;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tChart1 = new Steema.TeeChart.TChart();
this.btnAddSeries = new DevExpress.XtraEditors.SimpleButton();
this.SuspendLayout();
//
// tChart1
//
//
// tChart1.Header
//
this.tChart1.Header.Lines = new string[] {
"TeeChart"};
//
// tChart1.Legend
//
//
// tChart1.Legend.Title
//
//
// tChart1.Legend.Title.Font
//
this.tChart1.Legend.Title.Font.Bold = true;
//
// tChart1.Legend.Title.Pen
//
this.tChart1.Legend.Title.Pen.Visible = false;
this.tChart1.Location = new System.Drawing.Point(8, 8);
this.tChart1.Name = "tChart1";
this.tChart1.Size = new System.Drawing.Size(576, 576);
this.tChart1.TabIndex = 0;
//
// btnAddSeries
//
this.btnAddSeries.Location = new System.Drawing.Point(640, 16);
this.btnAddSeries.Name = "btnAddSeries";
this.btnAddSeries.Size = new System.Drawing.Size(96, 32);
this.btnAddSeries.TabIndex = 1;
this.btnAddSeries.Text = "Add Series";
this.btnAddSeries.Click += new System.EventHandler(this.btnAddSeries_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(888, 598);
this.Controls.Add(this.btnAddSeries);
this.Controls.Add(this.tChart1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private DataTable dt = null;
private void InitDataSet()
{
dt = new DataTable();
AddLabelColumn(dt);
}
private void AddLabelColumn(DataTable table)
{
table.Columns.Add(new DataColumn("Label", System.Type.GetType("System.DateTime")));
}
private void AddColumn(DataTable table, string columnName)
{
table.Columns.Add(new DataColumn(columnName, System.Type.GetType("System.Decimal")));
}
private void SetData(DataTable table)
{
Random rnd = new Random();
table.Rows.Clear();
for(int i = 0; i < 50; i++)
{
DataRow dr = table.NewRow();
foreach(DataColumn dc in table.Columns)
{
if(dc.DataType == System.Type.GetType("System.DateTime"))
{
dr[dc] = DateTime.Now.AddDays(i);
}
else if(dc.DataType == System.Type.GetType("System.Decimal"))
{
if((i < 15 || i > 40) && withNulls)
{
dr[dc] = DBNull.Value;
}
else
{
dr[dc] = Convert.ToDecimal(rnd.Next(10, 100));
}
}
}
table.Rows.Add(dr);
}
}
private void btnAddSeries_Click(object sender, System.EventArgs e)
{
if(dt == null)
{
InitDataSet();
}
if(dt.Columns.Count == 0)
{
AddLabelColumn(dt);
}
Steema.TeeChart.Styles.FastLine fl = new Steema.TeeChart.Styles.FastLine(tChart1.Chart);
AddColumn(dt, fl.Title);
SetData(dt);
fl.DataSource = dt;
fl.XValues.DataMember = "Label";
fl.XValues.DateTime = true;
fl.XValues.FillSequence();
fl.YValues.DataMember = fl.Title;
fl.IgnoreNulls = false;
fl.CheckDataSource();
tChart1.Series.Add(fl);
tChart1.Axes.Bottom.Labels.Style = Steema.TeeChart.AxisLabelStyle.Value;
}
}
}