Using SELECT DISTINCT on an already created DataTable object?
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 colu开发者_如何转开发mn 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
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.
You should use ToTable() on your default view (or any other view you are using) and provide true, to indicate you want distinct records:
DataTable distinctTable = originalTable.DefaultView.ToTable(true);
精彩评论