简体   繁体   中英

c# how do i check if datasource is null?

i have a control on a winform called chart1.

i would like to know whether chart1.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.

个人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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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