Using SELECT DISTINCT on an already created DataTable object?

Martin picture Martin · Nov 16, 2010 · Viewed 9.4k times · Source

I have an already created DataTable object which i am using for my girdview (asp.net) i also need to bind a column of that object to a DropDownList. The datatable has the correct details in the column but the column contains more that 1 of the same name in the column - hence I would love to just do some kind of SELECT DISTINCT on the datatable and copy it to a new datatable for use with binding the dropdown.

This would allow me to save resources by making another trip to the database.

Here is an example, the current datatable has a column called items and in this column has the following entries

1
1
1
1
5
5
6

And of course i need only unique items for binding to my dropdown, hence i need the following data

1
5
6

Of course i don't want to change the original datatable object but rather make a copy of it will the new details

Any ideas if this is possible ? Or do i need to make another trip to the db?

thanks in advance

Answer

Nitish Katare picture Nitish Katare · Nov 16, 2010

DataTable dt = new DataTable(); dt = dsMobileInfo.Tables[0].DefaultView.ToTable(true, "ColumnName");

//Applying the dvResult data set to the Grid for(int i=0;i

Hope This will work for you.