I am using a datagridview to show data in csv file. One of the column in datagridview in of numeric type [Column name: ID].
I am using autosort method of datagridview(sorting by clicking column header).
This works well for all the columns except this numeric column.
This column contains numbers 1 to 55
What I am getting now is after sorting is:
1,10,11,12,13,14,15,16,17,18,19,2,20,21,22,23,24,25,26,27,28,29,3,30,31...
...and so on. what I want is:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,...
Please help.
Thanks in advance.
You can use the event SortCompare like this:
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e) {
//Suppose your interested column has index 1
if (e.Column.Index == 1){
e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
e.Handled = true;//pass by the default sorting
}
}
Build your DataTable:
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Name");
dt.Columns.Add("ForeName");
I am using these columns due to the fact that I don't know what data you are storing.
After reading your csv-file you have to iterate through the data. Considering you have your data stored in a String array (String[][] arr) with the inner ArrayLength of 3 (ID, Name, ForeName).
for(int i=0;i<arr.Length;i++)
{
DataRow row = dt.NewRow();
row["ID"] = Convert.ToInt32(arr[i][0]);
row["Name"] = arr[i][1];
row["ForeName"] = arr[i][2];
dt.Rows.Add(row);
}
DataGridView dgv = new DataGridView();
dgv.DataSource = dt;
I hope this helps, it's a sample Code.
The reference to DataTable: https://msdn.microsoft.com/de-de/library/system.data.datatable(v=vs.110).aspx
EDIT
I just read that you already have a DataTable as result. You can build another DataTable as I did and iterate through your Data and convert the ID to an Integer. Please don't forget to set the Columns Type:
dt.Columns.Add("ID", typeof(Int32));
Well, your column is not numeric, but a text column that contains numeric string values. There's a huge difference! A real numeric column would apply numeric sorting, while your column uses alphanumeric sorting.
I'd try to actually change the underlying data type to int instead of string or apply manual sorting as suggested by lem2802.
Please add some information about how you fill the data grid view. Maybe that can help find an easier way than implementing a manual sort comparison.
According to your comment you create a DataTable as the data source for the grid view. You need to make sure that the column(s) containing numeric values also have a numeric type. In the code where you create the table and its columns, do something like this:
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(Int32));
... // Other columns
Later when you create the rows based on the content of your CSV file, make sure to fill each row so that the value for the ID column actually is an int:
DataRow row = table.NewRow();
row["ID"] = Convert.ToInt32(idValueFromCSV);
... // Other columns
That way you'll get what you want without implementing custom sorting.
I have a datarow from one table and trying to use that datarow to update the corresponding datarow in another datatable. I know I can loop thru and set each cell but was wondering if there is a sort merge functionality like there is datatables only just the individual datarow
If you have the indices of both of the rows you can just set the values of the two data row's item arrays to be equal to one another. For example:
DataRow firstTableRow = FirstTable.Rows[YourRow];
DataRow secondTableRow = SecondTable.Rows[YourOtherRow];
secondTableRow.ItemArray = firstTableRow.ItemArray;
I have datatable1 which has 11 columns and 100,000 rows.. I would like to do a check to see if the text in column one starts with "one" and if it does, add that row into the second datatable. I have done the below but yet still it does not work.. I get the error that the row belong to another table
foreach (DataRow r in queryDataTable.Rows)
{
if (r[0].ToString().StartsWith(queryString))
{
dt.ImportRow(r);
}
}
You cannot directly import the row of one table into another datatable. You need to create a new row and then copy the row.
Try this -
dt.Rows.Add(r.ItemArray)
Instead of your for loop, you may use LINQ to select those rows which StartsWith queryString and then you can use CopytoDataTable method to create a new table for the selected rows.
var NewTable = queryDataTable.AsEnumerable()
.Where(r => r.Field<string>(0).StartsWith(queryString))
.CopyToDataTable();
Remember to include using System.Linq; at the top.
i have a datable and like this i have searched a datarow from the datable on the basis of some primary now i want to add that searched row to another datatable how can i achieve this please let me know
DataTable findRows = (DataTable)ViewState["dt"];
List<int> selectedList=(List<int>)ViewState["selectedList"];
DataTable temp = new DataTable();
foreach (int id in selectedList)
{
DataRow dr=findRows.Rows.Find(id);
}
now i want it to add to datatable temp how can i achieve this?
First, when creating temp don't just instantiate it as a new DataTable but instead call .Clone() on findrows to create a structurally identical DataTable.
Second, use .ImportRow() on the second DataTable and pass it the row from the first DataTable that you'd like to copy. This should create an entirely new row in the second table with the same values as the row from the first table.
I have two DataTables.
First is
DataTable NameAddressPhones = new DataTable();
with Three columns Name, Address and PhoneNo.But I only want two columns Name and Address data so I want to copy those columns (with data) to the new DataTable.
DataTable NameAddress = new DataTable();
For that I do
foreach (DataRow sourcerow in NameAddressPhones.Rows)
{
DataRow destRow = NameAddress.NewRow();
foreach (string colname in columns)
{
destRow[colname] = sourcerow[colname];
}
NameAddress.Rows.Add(destRow);
}
I clear the NameAddressPhones(first) DataTable every time there are new records inserted in the table. And every time there will be the same number of columns but the column names will be different like Nm instead of Name, Add instead of Address.Now the problem is the second DataTable already has column names Name and Address and now I want to copy the columns data of Nm and Add to the second DataTable but the column names are different than the column names of the second DataTable. So even if there are different column names I want to copy Nm column data of first DataTable to the column Name of second DataTable and column Add data of first DataTable to column Address of second DataTable.
In short how can we copy column data from one DataTable to another even if there are different column names of both DataTables like Nm is the column name of first DataTable and Name is the column name of second DataTable then the data of the column Nm should be copied to the column Name.
Here's the simplest way:
foreach (DataRow sourcerow in NameAdressPhones.Rows)
{
DataRow destRow = NameAdress.NewRow();
destRow["Name"] = sourcerow["Nm"];
destRow["Address"] = sourcerow["Add"];
NameAdress.Rows.Add(destRow);
}
Automation is great when it's available. When it's not, you have to map source columns to destination columns in some manner.
If the columns are in the same order in both tables, you could just reference the values by ordinal instead of column name, but that's such a bad idea I'm not even going to post any code for it.
Use column index number rather than names:
destRow[0] = sourcerow[0]; // for column 0 = "Name" or "NM"
If I've understood your question right, then the way this is usually done is by using stored procedures. You have the same stored procedures in both databases, but the implementation is specific to the table schema of each database. This allows you the abstraction you need.