代码之家  ›  专栏  ›  技术社区  ›  Alex Gordon

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

  •  3
  • Alex Gordon  · 技术社区  · 14 年前

    我有一个名为chart1的winform控件。

    我想知道 chart1.DataSource

    我该怎么检查?

    4 回复  |  直到 14 年前
        1
  •  5
  •   DOK    14 年前

    如果数据源是 ,您可以首先检查数据表是否为空,然后检查其行数。Count>0。

    如果数据源是 先检查空值,然后检查表,然后检查行。

        2
  •  2
  •   stack72    14 年前

        3
  •  1
  •   Ian    14 年前

    检查它是否为空。

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

    如果您知道数据源是什么,那么可以对其进行强制转换并检查它是否为空。例如:

    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
       }
    }
    
        4
  •  1
  •   Neil Knight    14 年前
    if (chart1.DataSource == null)
    {
        // The DataSource is empty
    }