1
votes

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.

3
The column 'ID' is stored as a String. That's why it's sorted 1, 10, 11 ,... Can you post some code ?killexe
private void btnSort_Click_1(object sender, EventArgs e) { foreach (DataGridViewColumn column in Grid.Columns) { Grid.Columns[column.Name].SortMode = DataGridViewColumnSortMode.Automatic; } }Mr Aj
Look at the answer from @lem2802, I think it is exactly what you are searching for. The problem is, if you read a csv-file all the values are Strings. DataGridview just sorts Strings by their charactervalue.killexe
Can anybody comment about how to get data from csv to datagridview so that this kind of sorting can be done?Mr Aj
Yes, you can create a container (a DataTable for instance) and use this container as DataSource. Before storing the IDs in the DataTable you have to convert it to an Integer. I can post some code as answer if you want.killexe

3 Answers

1
votes

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));
2
votes

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
     }
}
0
votes

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.