Convert DataView to DataTable

serkan sendur 0 Tallied Votes 1K Views Share

Enables DataView to DataTable conversion

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)
            {
                Trace.WriteLine(ex.Message);
            }
            obNewDt.Rows.Add(dr);
        }

        return obNewDt;
    }
Aliasgher 0 Newbie Poster
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)            {                Trace.WriteLine(ex.Message);            }            obNewDt.Rows.Add(dr);        }         return obNewDt;    }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)
            {
                Trace.WriteLine(ex.Message);
            }
            obNewDt.Rows.Add(dr);
        }

        return obNewDt;
    }

i write above in my program to convert dataview to data table, but their is an error shows that
"Using the Generic type 'System.Collection.Generic.IEnumerable<T>' required '1' type argument
please help me how i can solve this problem
thanks in advance

bruce2424 0 Newbie Poster

thsi is vb code

Dim dTable As DataTable
dTable = dv.ToTable

for more details :

http://vb.net-informations.com/dataview/dataview-to-datatable.htm

thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.