I have a xtragrid on my form (let's say myXtraGrid)
And I bind a datatable:
myXtraGrid.DataSource = dtMyDataTable;
myXtraGrid.BestFitColumns();
Let's say I have just one column in datatable named "My Column" and it has three rows of data as below:
My Column
------
3
1
2
Than on grid (on runtime), I click on column header (My Column) and sort the data
(3 1 2) ascending: (1 2 3)
My question is; how can I get the sorted data as a Datatable as below?
My Column
------
1
2
3
On runtime I must get the sorted data and process it.
Thanks friends.
You can use DefaultView of Datatable and sort the view and later can be convert to table
if (dtMyDataTable.Rows.Count > 0)
{
DataView dv = dtMyDataTable.DefaultView;
dv.Sort = "cy_column ASC";
dtMyDataTable= dv.ToTable();
}
you can make a DataView.
myDataView dvMyView = new DataView(myDataTable);
This DataView can be sorted with:
dvMyView.Sort = " MyColumn ASC ";
So you have an sorted DataView which you can easily transform to a DataTable again:
myDataTable = dvMyView.toTable();
Now you have a sorted DataSource :-)
P.S In my opinion the BestPractice is to create an objectlist (List<myClass>). This Objectlist can be sorted. And an further advantage is that values changed in the Grid directly change your object in the List!
I hope that was helpful.
Related
For this project, I have to sort a DataTable by a certain column, the 'Checked' column. What this does is allow me to see the order in which each row was checked for any abnormalities. At the moment, this column looks like this:-
Checked
-------
0
2
3
4
6
7
10
1
11
12
5
8
9
The problem I have is that the table is not sorting by this column. Because of this, it checks rows that do not need to be checked. The code I currently use is:-
public void setTableData(DataTable table)
{
table.DefaultView.Sort = "Checked ASC";
table = table.DefaultView.ToTable();
tblProduct.DataSource = table;
}
After looking at different websites and from other programmers advice, I still cant see a problem with this code. What is causing the table to be unsortable?
what is your Checked Value? is it bool ?
for Sorting Data in DataTable you can try this :
DataView dv = table.DefaultView;
dv.Sort = "Checked desc";
DataTable sortedDT = dv.ToTable();
Your code for sorting is true , but we can't compile your code .I think its appear for data in Checked Value was wrong .
Can you post Checked Data or ?
How to filter two tables in C#? Table one contains full data and table Two contains some content of Table one?
What exactly do you want to do (it's not clear from your question)? Take table1, filter it and then pass the result to table2?
Then:
DataView dv = table1.AsDataView();
dv.RowFilter = fexpression; // for example "MyID = 3"
DataTable table2 = dv.ToTable();
// If you want typed datatable, you can do it like this (although there are other ways):
MyTypedDataTable table2 = new MyTypedDataTable();
DataTable tempTable = dv.ToTable();
table2.Merge(tempTable);
I've found this piece of code that can be used to get all distinct values. But my datatable has 10 columns. The distinctValues only shows the columns I write in the toTable(); Is it possible to use this function, but also show the rest of the columns?
DataView view = new DataView(table);
DataTable distinctValues = view.ToTable(true, "Column1", "Column2");
Unless those columns you mention are the full key to the table, there is no guarantee that for a particular combination of those two columns the other columns will have exactly one value.
And if they were the key, then there would be no need to use a "distinct" filter.
You can use Linq-To-DataTable
var distinct = from row in table.AsEnumerable()
group row by new
{
Col1 = row.Field<string>("Column1"),
Col2 = row.Field<string>("Column2")
} into Group
select Group.First()
DataTable tblDistinct = distinctRows.CopyToDataTable();
(assuming that you just want an arbitrary row[the first])
consider the code below:
DataView deletedLOV = new DataView(tbltmp, "prociLOV_Deleted=1", "prociLOV_ID",
DataViewRowState.CurrentRows);
DataView addedLOV = new DataView(tbltmp, "prociLOV_Id>1", "prociLOV_ID",
DataViewRowState.CurrentRows);
int deletedLOVcount=deletedLOV.Table.Rows.Count;
int addedLOVcount=addedLOV.Table.Rows.Count;
prociLOV_Deleted is set to 1 when a record is deleted.
But even when no records are deleted the deletedLOVcount return value 1. Also the same with addedLOVcount when there is no record with proci_ID>1 ,it too returns count as1
The DataView references it's original DataTable via the Table property. But it does not share the same data. The DataTable contains the original data without the applied filter whereas the DataView contains only the records after the appplied filter.
You would get the correct count via DataView.Count:
int deletedLOVcount = deletedLOV.Count;
MSDN:
Gets the number of records in the DataView after RowFilter and
RowStateFilter have been applied.
Having a DataView with different filters on it and made simple test with a ContextMexuStrip that reflects by simply right click the amount of DataView.Count exactly by choosen filter. So I confirm this is the right pick that represents available rows in the View after a filter is applied to all available rows in the view.
example code:
DataGridView myDGV = new DataGridView();
... ( set up your dgv )
DataView myDV = new DataView();
... ( add handlers if you need )
DataTable myDT = new DataTable(myDV);
myDGV.BindSource = myDT;
By adding to a contextmenustrip myCMS.Item.Add(" available entries : " + myDV.Count() ); you have it reflected.
I have DataTable object and am binding it to a gridview in C#.
I have 3 columns in the datatable, say "Flag", "Name", and "Value".
What I want to accomplish is that I want to only show the rows where "flag" fields are set to 0.
So say if I have two rows in the table,
Flag Name Value
------------------
0 tom 100
1 Jane 200
And, I only want to show "tom" and "100" on the gridview.
Is there any way I could do this without creating a new datatable?
Thanks.
Here is an example :
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
You can see example Here
Probably you can take the same Datatable like : table = table.Select(...);
try creating a DataView for your DataTable and send it to your GridView instead of the DataTable. see http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx