DataTable not sorting C# - c#

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 ?

Related

Assignment and Binding

I have a problem.Let's explain this:
I create a DataTable 'DataT' variable and added row to this variable.SameTime i have datagridview. After i assingmented 'DataT' to DataSource property of DataGridView.
I get 'DataT' Rows.Count property.It's value 10.But this value decrease from 10 to 5 when i removed 5 Rows of DataGridView.
So why decrease my value i just remove Rows of DataGridView but i did not remove rows of 'DataT' although rows count of 'DataT' decrease from 10 to 5.
I have not solve this problem.
EDIT:
Yes i bound datatable.But same problem there is other state.I explain with a example:
DataTable Table1 = new DataTable();
DataTable Table2 = new DataTable();
DataRow Rows;
private void Form1_Load(.......)
{
Tablo1.Columns.Add("Column1");
Tablo1.Columns.Add("Column2");
Rows = Table1.NewRow();
Rows[0] = "Hello";
Rows[1] = "Word";
Table1.Rows.Add(Rows);
Rows = Table1.NewRow();
Rows[0] = "Hello";
Rows[1] = "Word";
Table1.Rows.Add(Rows);
Table2 = Table1;
datagridview1.DataSource = Table1;
//This datagridview1 has 2 Rows and Table1 has 2 Rows.
datagridview1.Rows.RemoveAt(0);
//I am Removing one Row of datagridview1.Not from Table1.
//But Automatic removing Rows from Table1.
//Result=datagridview1 has 1 Row and Table1 has 1 Row.Why do remove rows from Table1?
//Even Rows Remove from Table2 when i remove rows from datagridview1.
}
As Henk Holterman pointed out in comment there are two reasons making Table2 to change
since Table1 is used as DataSource for grid any changes in the grid will be reflected in it. This is expected behavior of GridView.
GridView has its own Rows collection that is updated when one sets DataSource. Any changes to Rows collection (like removing item) are applied to DataTable set as data source. As result datagridview1.Rows.RemoveAt(0); actually indirectly removes row from the object referenced by Table1.
Both of your variables (Table1 and Table2) point to the same object - so whenever anything happens to the DataTable (i.e. row removed by GridView) the change is visible when you check either of variables.
Following code performs "shallow copy" when you seem to expect complete clone similar to behavior of int:
Table2 = Table1;
After that assignment both Table1 and Table2 refer to DataTable created on DataTable Table1 = new DataTable(); line.
See more What is the difference between a deep copy and a shallow copy?

Fill DataGridView on condition from database

I have MySQL database and a DataGridView in C# and to fill the DataGridView I do the following:
schoolDataSet schl = new schoolDataSet();
schoolDataSetTableAdapters.studentinfoTableAdapter adptr = new schoolDataSetTableAdapters.studentinfoTableAdapter();
adptr.Fill(schl.studentinfo);
dataGridView1.DataSource = schl.studentinfo.DefaultView;
and undesired columns I make them visible = false from DataGridView properties but I came with a problem if I want to specify what data (rows) to fill in DataGridView such applying a where condition like:
fill data in DataGridView WHERE IsActive = 1 so can I still use the above code with some modifications or I have to write SQL query and fill the DataGridView manually ?
After searching and trying tons of codes I got it as following in simplest code:
In the code above just comment out last line which is dataGridView1.DataSource = schl.studentinfo.DefaultView; or simply replace it with the following
DataView dv = new DataView(schoolDataSet.studentinfo, "IsActive = 'false'", "id", DataViewRowState.CurrentRows);
Which creates a new DataView and filters according to IsActive column with false value, the third parameter id is to sort based-on, and finally you can write another line
dataGridView1.DataSource = dv; that will tell the DataGridView to load data from DataView.
Hope to save someone's time.
Big thanks goes to #Karthik Ganesan

Joining Values Of Two Columns From DataTable

Joining Values Of Two Columns From DataTable
Two columns make it in one columns from datatable
my datatable is
TagNumber, LogNumber Combined
124 1 2
125 1 3
126 2 4
o/p:
TagNumber
124 ~1~2
125 ~1~3
126 ~2~4
combined column is merge from column0 and column1
i dont understand hw can i do please write sample of code
I dont have experience on linq .
I add column bt hw can i merge two columns in that one columns
I got answer:
For i As Integer = 0 To dstemp.Tables(0).Rows.Count - 1
dstemp.Tables(0).Rows(i)(0) = dstemp.Tables(0).Rows(i)("TagNumber") & "~" & dstemp.Tables(0).Rows(i)("LogNumber") & "~" & dstemp.Tables(0).Rows(i)("Combined")
next
Try doing it like this.
dtTwoItems.Columns.Add("Combined", typeof(string), "TagNumber+'/'+LogNumber");
Ok if you really want to do this then you have create a extra DataTable with Three Columns:
TagNumber, LogNumber Combined:
as below:
private DataTable CreateDataTableColumns()
{
DataTable dtThreeItems = new DataTable();
dtThreeItems.Columns.Add("TagNumber", typeof(String));
dtThreeItems.Columns.Add("LogNumber", typeof(String));
dtThreeItems.Columns.Add("Combined", typeof(String));
return dtThreeItems;
}
Now iterate the old datatable as below to get combined value:
foreach (DataRow dr in dtTwoItems.Rows)
{
row = dtThreeItems.NewRow();
row["TagNumber"] = dr["TagNumber"].ToString();
row["LogNumber"] = dr["LogNumber"].ToString();
row["Combined"] = dr["TagNumber"].ToString()+"/"+dr["LogNumber"].ToString() ;
dtThreeItems.Rows.Add(row);
}
Thats All
DataTable is like a container.
It is not proper to join tables.
I recommend you'd use linq.
Did you try to Add new Column to DataTable and then iterate through Each Row to put value by combining them?
EDIT: I Am not sure if Linq or Datatable query have some inbuilt feature to do this, but simple solution is what I tell. Or if you are filling your datatable from any SQL Query based database, then write a SQL that has third column with merged value using concat of columns.
Edit2:
foreach (Datarow r in myTable.Rows) {
r["Newcolumn"] = Convert.ToString(r["c1"]) + "/" + Convert.ToString(r["c2"]);
}

Xtra Grid Sorted Data on runtime?

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.

Show only selected rows in Gridview

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

Categories

Resources