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.
Related
I am using DataTable. In which I want to sort on particular columns without creating copy. I have also tried with DataTable.Select() but it required new table for sorted output which will be not worked for me as my source datatable is bound with control.
You can take any example of DataTable
So how can I achieve the task?
Below code will help you to achieve your requirement
IEnumerable<DataRow> data = from dt in dtTemp.AsEnumerable()
orderby dt.Field<Int32>("IDColumn"), dt.Field<String>("NameColumn") ascending
select dt;
Below code as an example:
DataTable dt = new DataTable();
//Populate dt here
dt.DefaultView.Sort = string.Format("{0} {1}", "column name here", "ASC"); //sort ascending
dt = dt.DefaultView.ToTable();
I am setting my gridview.datasource into a datatable variable as below:
DataTable dt = gvPaymentHistory.DataSource as DataTable;
The gvPaymentHistory.DataSource has a record, however, the dt is null after that line has executed. How can I pass the Datasource records to dt?
EDIT
DataSource is List collection of a class object. It's not a DataSet
Simple way is to store the data source of the grid in view source when u r binding the data to the grid and then retrieve it from the view source every time you need it.
Gridview.Datasource = yourdatasource;
ViewState["mydatasource"] = yourdatasource;
While retrieving
DataTable dt = ViewState["mydatasource"] as DataTable;
Hope this solves your problem.
Try This Way: Use BindingSource
BindingSource bs = (BindingSource)gvPaymentHistory.DataSource;
DataTable dt =((YourDataSetType) (bs.DataSource)).Tables[0];
EDIT
if the bidning type is list than try out
List<CodeEntity> data= gvPaymentHistory.DataSource as List<CodeEntity>;
or
List<CodeEntity> codes = (List<CodeEntity>)gvPaymentHistory.DataSource;
check this
if(gvPaymentHistory.DataSource is DataTable)
DataTable dt = gvPaymentHistory.DataSource as DataTable;
if(gvPaymentHistory.DataSource is DataView )
DataView dv = gvPaymentHistory.DataSource as DataView;
I am having a dataset (Dataset ds) and below you can find the field with demo data inside the dataset.
In my Dataset , their is column with name Date (Datatype- DateTime) and i want to sort this column.I cant do the sorting from SQl because the Dataset is a merge of 2 Different Dataset.
Please help me that how i do the sorting in dataset.
Date Volume
07/19/201211:30AM 12
07/18/201201:30PM 13
07/17/201203:30PM 22
This is a simple Linq-To-DataSet approach:
IEnumerable<DataRow> orderedRows = dataSet.Tables[0].AsEnumerable()
.OrderBy(r => r.Field<DateTime>("Date"));
Now you can use that as DataSource or enumerate it in a foreach. If you need to materialize it, you might want to create a new DataTable from it:
DataTable tblOrdered = orderedRows.CopyToDataTable();
You can create a data view from your data-set.
Then you can use the DataView.Sort Property to sort your data.
eg.
DataView myDataView = DT.DefaultView;
myDataView.Sort = "Date DESC";
Read more about Introduction to Filtering and Sorting in Datasets
Using Dataview you can sort your Datatable.
Just put your Datatable in Dataview
Look at the following example :
You have dtlog table which having two columns one is date and another one is detials.
you want to sort your datatable by date column.
DataTable dtlog = new DataTable();
dtlog.Columns.Add("date");
dtlog.Columns.Add("details");
1.create dataview and put your datatable in view
DataView dtview = new DataView(dtlog);
string sortstring = "date DESC"; // sorting in descending manner
dtview.Sort = sortstring;
2.Create another table where you can save your sorted dataview
DataTable dtsort =dtview.ToTable();
You can do this by a DataView. If your column which is holding datetime is a datetime column
DataView dv = DataTable.DefaultView;
dv.Sort = "DateTime-ColumnName ASC";
It will not work on string column holding datetime as string.
The DataView of a DataTable have a sort property please pass the column name to it
E.g.
dataSet1.Table[0].DefautlView.Sort = "ColumnName";
default sort is by ascending order
For more see link http://msdn.microsoft.com/en-us/library/system.data.dataview.sort(v=vs.100).aspx
I find my solucion is add more columns to Datatable where we can save each part of a DateTime:
Tabla.Columns.Add("year", typeof(DateTime));
Tabla.Columns.Add("month", typeof(DateTime));
Tabla.Columns.Add("day", typeof(DateTime));
then we can sort our datatable with a DataView:
DataView myDataView = DT.DefaultView;
case 1: ASC
myDataView.Sort = "year asc, month asc, day asc";
case 2: DESC
myDataView.Sort = "year desc, month desc, day desc";
Tabla = myDataViwe.toTable();
result is our datatable sorted for a dateTime
It worked for Me:
dataTableEmpSurvey.DefaultView.Sort = "SNO";
var dataTableSorted = dataTableEmpSurvey.DefaultView.ToTable();
https://www.codeproject.com/Questions/219205/How-To-Sort-Datatable-in-csharp-net
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;
I'm using the following code to populate a DataGridView (sqliteAdapter derives from DbDataAdapter):
sqliteAdapter.SelectCommand.CommandText = SQLCommand;
sqliteConn.Open();
using (DataTable dt = new DataTable())
{
sqLiteAdapter.Fill(dt);
dataGridRes.DataSource = dt;
}
The Actual SQLCommand is:
SELECT Email NOTNULL AS Sel, Regiao, Distrito, Grupo, MG, ID, Nome, Morada, Email
FROM assessores
Now, I would like to reuse this DataTable to populate other controls in the form. Namely 4 CheckedListBox controls. I was hoping to avoid any further connections to the database by "filtering" the current DataTable to something like (Invalid code. Illustrative only)
SELECT Distinct Regiao FROM DataTable
SELECT Distinct Distrito FROM DataTable
SELECT Distinct Grupo FROM DataTable
SELECT Distinct MG FROM DataTable
My forays into adapter and DataTable properties and methods have been fruitless.
Controls can be bound to any collection, not just a DataTable.
You can therefore use LINQ, like this:
myControl.DataSource = dt.AsEnumerable().Select(dr => dr.Field<string>("Regiao")).Distinct().ToArray();
You can filter a DataTable but it returns an array of DataRow objects rather than a filtered DataTable, which can't be directly bound to a DataGrid or other data bound control. Instead use a DataView:
DataView dv = new DataView(dt);
dv.RowFilter = "Filter Expresion";
dg.DataSource = dv;
See Sorting and Filtering Data Using a DataView