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