2016-06-21 89 views
2

所以我只想將值賦給'originalCOlumName'如果dataStore.DataSourceDef.Rows[columnIndex].ItemArray[3].ToString();中有一個值,如果它是NULL或超出範圍,不存在等等...我想跳過這個部分。 伊夫試圖尋找另一個例子在Preventing Index Out of Range Error索引超出範圍檢查器不能正常工作

但NULL跳棋沒有工作也試過

if(string.IsNullOrEmpty(dataStore.DataSourceDef.Rows[columnIndex].ItemArray[3].ToString()) 
+0

如果你得到一個索引超出範圍的異常,你需要檢查的長度或計數Rows'的'屬性,然後'ItemArray'你可能需要在除了覈對DBNull.value到您的支票 –

+3

針對您正在使用的索引。 – juharr

回答

0

您需要檢查行和ItemArray集合兩者的長度,以確保他們有足夠的元素來索引...記住,要獲取元素編號3,數組必須包含4個項目,因爲編號從0開始。

var rowsLength = dataStore.DataSourceDef.Rows.Count; 
if (rowsLength >= columnIndex + 1){ 
    var itemArrayLength = dataStore.DataSourceDef.Rows[columnIndex].ItemArray.Count; 
    if (itemArrayLength >= 4){ 
     var theString = dataStore.DataSourceDef.Rows[columnIndex].ItemArray[3].ToString(); 
    } 
} 
+2

你可以只做'columnIndex juharr