c# how do i check if datasource is null?
i have a control on a winform called chart1.
i would like to know whether chart开发者_如何转开发1.DataSource
is empty
how do i check it?
If the DataSource is a DataTable, you can check first that the DataTable is not null, and secondly that its Rows.Count > 0.
If the DataSource is a DataSet, you check for null, then tables, then rows.
Check to see if it is null.
if(chart1.DataSource == null)
{
// Do something
}
If you know what the DataSource is, then you can cast it and check to see if it is empty or not. For example:
List<String> strings = new List<String>() { "a", "b" };
// Set chart1.DataSource to strings... then later on
if(chart1.DataSource != null)
{
List<String> boundStrings = chart1.DataSource as List<String>;
if(boundStrings != null && boundStrings.Count > 0)
{
// do something
}
}
personally id check the datasource for null BEFORE I bind it to chart so I don't have to worry about chart1 dealing with a null datasource
if (chart1.DataSource == null)
{
// The DataSource is empty
}
精彩评论