简体   繁体   English

c#如何检查数据源是否为空?

[英]c# how do i check if datasource is null?

i have a control on a winform called chart1. 我控制了一个名为chart1的winform。

i would like to know whether chart1.DataSource is empty 我想知道chart1.DataSource是否为空

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. 如果DataSource是DataTable ,则可以先检查DataTable是否为null,其次是Rows.Count> 0。

If the DataSource is a DataSet, you check for null, then tables, then rows. 如果DataSource是DataSet,则检查null,然后检查表,然后检查行。

个人id检查数据源为null之前我将它绑定到图表所以我不必担心chart1处理null数据源

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. 如果您知道DataSource是什么,那么您可以投射它并检查它是否为空。 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
   }
}
if (chart1.DataSource == null)
{
    // The DataSource is empty
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM