[英]c# how do i check if datasource is null?
我控制了一個名為chart1的winform。
我想知道chart1.DataSource
是否為空
我該如何檢查?
如果DataSource是DataTable ,則可以先檢查DataTable是否為null,其次是Rows.Count> 0。
如果DataSource是DataSet,則檢查null,然后檢查表,然后檢查行。
個人id檢查數據源為null之前我將它綁定到圖表所以我不必擔心chart1處理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
}
}
if (chart1.DataSource == null)
{
// The DataSource is empty
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.