1

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.

8
  • The column 'ID' is stored as a String. That's why it's sorted 1, 10, 11 ,... Can you post some code ? Commented Aug 4, 2015 at 11:57
  • private void btnSort_Click_1(object sender, EventArgs e) { foreach (DataGridViewColumn column in Grid.Columns) { Grid.Columns[column.Name].SortMode = DataGridViewColumnSortMode.Automatic; } } Commented Aug 4, 2015 at 12:00
  • 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. Commented Aug 4, 2015 at 12:03
  • Can anybody comment about how to get data from csv to datagridview so that this kind of sorting can be done? Commented Aug 4, 2015 at 12:14
  • 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. Commented Aug 4, 2015 at 12:20

3 Answers 3

2

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
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Tried! Code for this event is unreachable ! Donno why? :(
@MrAj did you add the event to the DataGridView? For example: dvg.OnSortCompare += dataGridView1_SortCompare; dvg is the DataGridView.
Where should i add this event? OnFormLoad ? Previously, I added this event by clicking in grid->properties->events->setcompare. Is that enough?
1

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

Comments

0

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.

3 Comments

I am using private DataTable loadCSVDataTable(string strCsvFilePath) { using TextFieldparser i am returning datatable }
And then Grid.DataSource = dtAllTableData.DefaultView;
Then you should change the definition of your DataTable (the part where you create the columns) to create an Int32 column. I'll edit my answer to show you an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.