C # how to check if a data source is null?

I have a winform control called chart1.

I would like to know if it is empty chart1.DataSource

how to check it?

+3
source share
4 answers

If the DataSource is a DataTable , you can first check that the DataTable is not null, and secondly, its Rows.Count> 0.

If the DataSource is a DataSet, you check for null, then tables, and then rows.

+5
source

null , 1,

+2

Check if it is null.

if(chart1.DataSource == null)
{
 // Do something
}

If you know what a DataSource is, you can drop it and check if it is empty or not. For instance:

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
   }
}
+1
source
if (chart1.DataSource == null)
{
    // The DataSource is empty
}
+1
source

Source: https://habr.com/ru/post/1773267/


All Articles