1

I'm using DatagridView in to read a csv file.
The data is not sorting properly when clicking the headers at the top of the grid.

For example, it should be sorted as 1,2,3,4,5,etc
However, the result is 1,10,2,20,etc.

I think I need to convert it from a string to an int, but I don't know how to do that.

Here's my current code:

try
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string csvPath = openFileDialog1.FileName;
        string rowValue;
       // int rowValue = int.Parse(??);
        string[] cellValue;
        dataGridView1.Rows.Clear();
        //dataGridView1.Columns.Clear();
        if (System.IO.File.Exists(csvPath))
        {
            System.IO.StreamReader fileReader = new StreamReader(csvPath);
            rowValue = fileReader.ReadLine();
            cellValue = rowValue.Split(',');

            for (int i = 0; i <= cellValue.Count() - 1; i++)
            {
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                column.Name = cellValue[i];    //column name , value
                column.HeaderText = cellValue[i];
                dataGridView1.Columns.Add(column);
               // dataGridView1.Columns[].CellType = typeof(Int64);
            }                      
            while (fileReader.Peek() != -1)
            {
                rowValue = fileReader.ReadLine();
                cellValue =  rowValue.Split(',');
                dataGridView1.Rows.Add(cellValue);
            }
            fileReader.Dispose();
            fileReader.Close();
        }
        else
Rob
  • 25,569
  • 15
  • 73
  • 87
Simkyujin
  • 95
  • 2
  • 2
  • 8

2 Answers2

0

Try to register a comparer into the DataGridView like this:

private void CustomSortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
    if(e.Column.Name != "CollumnName") return;

    var a = int.Parse(e.CellValue1.ToString());
    var b = int.Parse(e.CellValue2.ToString());

    // If the cell value is already an integer, just cast it instead of parsing    
    e.SortResult = a.CompareTo(b);    
    e.Handled = true;
}

and the register the sorter in the DataGridView

dataGridView1.SortCompare += CustomSortCompare;

See the documentation DataGridView.SortCompare Event

Joel R Michaliszen
  • 3,934
  • 1
  • 16
  • 24
  • ok, thanks...i have new problem... first column's header ok 1,2,3,4,5,6,7,8,9,10 good ! but others.. have error... – Simkyujin Dec 17 '15 at 03:17
  • @Simkyujin You can add a sort comparer in each collumn, so you can verify if it is the collumn that you want like this ````e.Column.Name != "CollumnName"```` and then just return. If it was helpul please dont forget to helpful vote or mark the answer as the correct. – Joel R Michaliszen Dec 17 '15 at 03:34
  • ok! i will try.. very thank you ! have a nice day! – Simkyujin Dec 17 '15 at 04:21
0

You may use this code to detect the column of numbers in string mode, convert them to integers and sort them:

try {
if (e.Column.Index == 2) {
    e.SortResult = Convert.ToInt32(e.CellValue1) < Convert.ToInt32(e.CellValue2) ? -1 : 1;
    e.Handled = true;
    }

  } catch (Exception ex) {
  }

In the above sample, the numbers are in the column 2 of Datagridview. Put the code into the SortCompare event of grid.

David BS
  • 1,614
  • 1
  • 14
  • 26