In DataTable I could sorting with
dataTable.DefaultView.Sort = "SortField DESC";
I'm getting a DataSet from database, I was wondering could I do a sorting on the DataSet like how I do it in DataTable.
you can still access the DataTable from the the data set as follows,
ds.Tables[0].DefaultView.Sort =" criterian";
Hope this helps.
DataView view = ds.Tables[0].DefaultView;
view.Sort = "SortField DESC";
http://msdn.microsoft.com/en-us/library/1ay5y4w0(v=vs.71).aspx
http://social.msdn.microsoft.com/Forums/nl/netfxbcl/thread/adbd95cd-49d1-483d-b2b2-4b696a66e9a6
Access the DataTable from the the DataSet as follows,
ds.Tables[0].DefaultView.Sort = "SortField DESC";
Hope this helps.
For advanced sorting needs you might want to use LINQ like described here. Basically it allows creating a DataView from a LINQ query using the System.Data.DataTableExtensions.AsDataFiew extension method.
Alternatively if you are OK with (or maybe even prefer) using an IEnumerable
you could use the System.Data.DataTableExtensions.AsEnumerable extension method. For example:
var enumerable = dataSet.Tables[0].AsEnumerable()
.OrderBy(x => x.Field<string>("ColumnName")
.ThenByDescending(x => x.Field<int?>("OtherColumnName")??0);
From tha DataSet object, you can access all the DataTable to intract with.
Try this:
DataDet.Tables[0].DefaultView.Sort = "sort criteria";
DataSet fileTransferDetail = null;//Data to be sorted.
DataSet result = null;//Declare a dataSet to be filled.
//Sort data.
fileTransferDetail.Tables[0].DefaultView.Sort = "ID DESC";
//Store in new Dataset
result.Tables.Add(fileTransferDetail.Tables[0].DefaultView.ToTable());
Try the following code.
DataView dv = new DataView();
dv = ds.Tables[0].DefaultView;
dv.Sort=value;
Related
I have a DataTable (Ado.Net) with column 'Status'. This column holds the values (in each records)
['Red','Green','Blue','Yellow','White','OtherColors']
I want to select all the rows which status value not Red,Green,Blue
What the kind of filter expression to use to select data with my proposed criteria. So i want to achive some thing like we used in sql query ( WHERE Status NOT IN ('Red','Green','Blue')
NB:This project is running .NET 2.0 i cant use linq
I have tested it, it works as desired:
DataRow[] filtered = tblStatus.Select("Status NOT IN ('Red','Green','Blue')");
The resulting DataRow[] contains only DataRows with OtherColors, Yellow and White.
If you could use LINQ i'd prefer that:
string[] excludeStatus = {"Red","Green","Blue"};
var filteredRows = tblStatus.AsEnumerable()
.Where(row => !excludeStatus.Contains(row.Field<string>("Status")));
Without Linq you can use the rowfilter of a DataView like this
public DataTable GetFilteredData(DataTable table, string[] filterValues)
{
var dv = new DataView(table);
var filter = string.join("','", filterValues);
dv.RowFilter = "Status NOT IN ('" + filter + "')";
return dv.ToTable();
}
Supposing your datatable is part of a typed dataset you can use Linq to datasets, from which you could something like:
var records =
from record in datatable
where !record.Status.Contains('Red','Green','Blue')
select record;
Even if you don't have a typed dataset Linq to datasets is your answer. You would need to some casting, but not to difficult.
I have a dataset that i need to add into a dataset. I need to sort datatable before adding it into dataset.I can use DataView to sort the datatable but cannot figure out how to add this dataview into the dataset. Any ideas
DataView myDataView = MyDataTable.DefaultView;
myDataView.Sort = " Key1, Key2"
MyDataSet.Tables.Add(myDataView); // no overload that takes a dataview
You can create a DataTable by calling DataView.ToTable.
I want a user to have different "views" of a TableAdapter. These do not need to be committed back to the database.
I have tried something like the following
mytba.DataView = someOtherDataView;
with no success.
Is it possible to do this?
You need to create a new DataView from the underlying DataTable.
It's been a while since I used TableAdapters, but you can fill a data table with it
DataTable dt = new DataTable();
MyTableAdapter.Fill(dt);
DataView dv1 = new DataView(dt);
DataView dv2 = new DataView(dt);
I have a DataTable named dt2 with data. I am calling its Select method to get some specific rows.
DataRow[] foundRows;
expression = "parent_id=1";
foundRows = dt2.Select(expression);
How can I pass the Select-method result to a new DataTable – say FilteredData?
You can use CopyToDataTable, available on IEnumerable<DataRow> types.
var filteredData = dt2.Select(expression).CopyToDataTable();
Just for clarity, the Select method returns an array of type DataRow. That's why we need to use CopyToDataTable(). Alex's answer is good. However, if the Select didn't return any rows, CopyToDataTable() will throw an InvalidOperationException.
So test that there is at least one DataRow before using the CopyToDataTable().
var filteredDataRows = dt2.Select(expression);
var filteredDataTable = new DataTable();
if(filteredDataRows.Length != 0)
filteredDataTable = filteredDataRows.CopyToDataTable();
Why not use a DataView instead?
DataView view = new DataView(dt2);
view.RowFilter = "parent_id = 1";
DataView will behave in very much the same way that a DataTable would with the added benefit that any change(s) to the underlying DataTable (dt2 in this case) would be automatically reflected in the DataView.
I generate a DataTable (from non-SQL data) and then use a DataView to filter the records.
I want to limit the number of records in the final record set but can't do this when I generate the DataTable.
I've resorted to deleting rows from the final result set, as per:
DataView dataView = new DataView(dataTable);
dataView.RowFilter = String.Format("EventDate > '{0}'", DateTime.Now);
dataView.Sort = "EventDate";
dataTable = dataView.ToTable();
while (dataTable.Rows.Count > _rowLimit)
dataTable.Rows[dataTable.Rows.Count - 1].Delete();
return dataTable;
Is there a more efficient way to limit the results?
You can use Linq:
Try changing your code to the following:
DataView dataView = new DataView(dataTable);
dataView.RowFilter = String.Format("EventDate > '{0}'", DateTime.Now);
dataView.Sort = "EventDate";
dataTable = dataView.ToTable();
while (dataTable.Rows.Count > _rowLimit)
{
dataTable = dataTable.AsEnumerable().Skip(0).Take(50).CopyToDataTable();
}
return dataTable;
You'll need namespace: System.Linq and System.Data
Can you add a column which will be similar like rowcount 1,2,3
Something like incrementing as you add rows from your Non-SQL data to the datatable.
Probably after that you can use
DataRow[] rows1 = dTable.Select(" RowIncrement < 50", "EventDate ASC");
RowIncrement is the column which would be something like ROWNUM
Try copying the top x rows from one datatable into a new one.
dataTable.AsEnumerable().Take(50).CopyToDataTable(newdataTable)
and then use that.