DataView 转成DataTable

代码
        public 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)
                {
                    
throw new Exception("DataView create table error!");
                }
                obNewDt.Rows.Add(dr);
            }

            
return obNewDt;
        }

 

posted on 2010-03-26 13:32  AndyCai  阅读(198)  评论(0编辑  收藏  举报

导航