Possible Duplicate:
Convert ArrayList into string array(string[]) in c#我如何將arraylist轉換爲字符串[]?
如何將數組列表轉換爲string[]
?下面是我正在嘗試的代碼。我得到一個錯誤:
At least one element in the source array could not be cast down to the destination array type.
ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in DtSet.Tables[0].Rows)
{
myArrayList.Add(dtRow);
}
string[] myStringArray = (string[])myArrayList.ToArray(typeof(string));
你仍然使用.NET 1.1。或者你爲什麼不使用鍵入'List'而不是'ArrayList'? –
2012-07-16 09:24:41
「源數組中至少有一個元素不能被轉換爲目標數組類型。」它真的是在這裏告訴你這個問題......善良的;源數組中的每個「元素」都是一個「DataRow」。它告訴你它不能將'DataRow'強制轉換爲'string'。這是完全正確的; DataRow **不是**字符串...... – 2012-07-16 09:32:14
您試圖將DataRows轉換爲字符串。您需要首先檢查是否允許您使用的行。如果是這樣,你可以簡單地遍歷並分別處理它們並將其添加到字符串數組中。但我懷疑這個演員會成功。 – 2012-07-16 09:33:07