我有一个名为chart1的WinForm控件。
我想知道chart1.DataSource是否为空。
如何检查呢?
我想知道chart1.DataSource是否为空。
如何检查呢?
检查是否为null。
if(chart1.DataSource == null)
{
// Do something
}
如果您了解DataSource是什么,那么您可以对其进行强制类型转换并检查其是否为空。例如:
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
}
}
个人建议在将数据源绑定到图表之前先检查其是否为空,以便不必担心chart1处理空数据源的情况。
if (chart1.DataSource == null)
{
// The DataSource is empty
}
if(chart1.DataSource == null) {}
... 或者你所说的“空”是指其他什么? - Dan J