Convert DataView to DataTable

zhaozj2021-02-16  100

public static DataTable GetDataTable (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);} returnobnewdt;}

转载请注明原文地址:https://www.9cbs.com/read-11117.html

New Post(0)