Search code examples
c#datatabledataview

C# - Rename DataView Columns


I'm working with DataView. I copy some columns from a datatable and insert it into DataView. Due to some requirements, I need to add some duplicate columns. But, It through the DuplicateColumnName Exception. Can you please help. Is there any other way to copy DataView data to DataTable?

stringarr = new string[] { "ML#", "Address", "Purchaser","Address","Purchaser", "Sold Price", "State", "Taxes", "Town", "Zip", "District", "Section", "Block", "Lot", "Owner" };

    DataTable distinctValues = new dataTable();
    if (cbMLSDataType.SelectedIndex == 1)
        {
            distinctValues = view.ToTable(false, stringarr);
        }
        else if (cbMLSDataType.SelectedIndex == 2)
        {
            distinctValues = view.ToTable(false, stringarr);
        }

Solution

  • To copy the values of the view.

    view.ToTable()
    

    To copy the source view but not the filtered data of the DataView.

    view.Table.Copy()
    

    To copy the structure of the source DataTable.

    view.Table.Clone()