One of the problems in the project last week was to get the value of a column in the DataTable, since retrieving data from a database is grouped by 2 fields, and the column to be obtained is exactly in the two columns of the grouping, so the value of the column is bound to be duplicated, so I think of removing duplicates, With the idea after the online look at some of the methods, mostly traversal, although the function can be achieved, but the efficiency is too low, and finally found a simple method, as follows:
public string[] Getnamesfromdatatable (DataTable DataTable)
{
DataView dv = Datatable.defaultview;
dataTable = dv. ToTable (True, "Name");
string[] names = new string[ DataTable.Rows.Count];
for (int i = 0; i < names. Length; i++)
{
Names[i] = datatable.rows[i][0]. ToString ();
}
return names;
}
Parsing: The Dataview.totable () method has four overloaded methods, respectively, as follows:
ToTable (): Creates and returns a new DataTable based on the rows in the existing DataView
ToTable (string tableName): function as above, just to assign a name to the new DataTable
Todatatable (bool Distinct,parm string[] columnnames): Creates and returns DataView based on rows in existing datatable,distinct, true to return rows with different values for all columns , the second parameter is an array of characters, that is, you can specify the column to get, in the example above, only one column is specified, that is, to get the non-duplicated information for a column.
Todatatable (String Tablename,bool distinct,parm string[] columnnames); function as above, just one more datatablename
How to remove duplicate item "Go" in a DataTable