因为Framework2.0开始DataView 有toTable()方法,可以直接转。但vs2003还是Framework1.1 没有这个方法。
所以要手动转,方法如下:
public static DataTable CreateTable(DataView obDataView)
{
if (null == obDataView)
{
throw new ArgumentNullException("DataView", "Invalid DataView object specified");
}
DataTable obNewDt = obDataView.Table.Clone();
int idx = 0;
string[] strColNames = new string[obNewDt.Columns.Count];
foreach (DataColumn col in obNewDt.Columns)
{
strColNames[idx++] = col.ColumnName;
}
IEnumerator viewEnumerator = obDataView.GetEnumerator();
while (viewEnumerator.MoveNext())
{
DataRowView drv = (DataRowView)viewEnumerator.Current;
DataRow dr = obNewDt.NewRow();
try
{
foreach (string strName in strColNames)
{
dr[strName] = drv[strName];
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
obNewDt.Rows.Add(dr);
}
return obNewDt;
}