简体   繁体   中英

Sorting N-digit numbers with Sort Method in dataGridview in C# not working

I have a dataGridview with some information. I will sort the rows to T(Period) . I want the result to be: ACBD .

But this method is not working. It seems that only the first digit is compared. I have no idea what to do.

Thanks for any help.

dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Ascending);

在此处输入图片说明

if (openFile.ShowDialog() == DialogResult.OK)
{
    var items = File.ReadLines(openFile.FileName).Select(line => line.Trim().Split(' '));
    DataTable dt = new DataTable();
    foreach (var line in items)
    {
        while (line.Length > dt.Columns.Count)
            dt.Columns.Add(new DataColumn($"Column {dt.Columns.Count}", typeof(string)));
        dt.Rows.Add(line);
    }
    dataGridView1.DataSource = dt;
    /////////////////////////
    dataGridView1.Columns[0].HeaderCell.Value = "Tasks";
    dataGridView1.Columns[1].HeaderCell.Value = "Computation Time";
    dataGridView1.Columns[2].HeaderCell.Value = "T (Period)";
    dataGridView1.Columns[3].HeaderCell.Value = "Deadline";
    dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Ascending);
}

I read the data from a txt file like below:

A 1 3 3
B 1 6 6
C 1 5 5
D 2 10 9

You need to set the columns with integers as a typeof(int) to get the proper sorting of integer values instead of string values. Example…

  if (openFile.ShowDialog() == DialogResult.OK) {
    DataTable dt = new DataTable();
    dt.Columns.Add("Tasks", typeof(string));
    dt.Columns.Add("Comparison Time", typeof(int));
    dt.Columns.Add("T (Period)", typeof(int));
    dt.Columns.Add("Deadline", typeof(int));
    var items = File.ReadLines(openFile.FileName).Select(line => line.Trim().Split(' '));
    foreach (var line in items) {
      dt.Rows.Add(line);
    }
    dataGridView1.DataSource = dt;
    dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Ascending);
  }

You can implement a custom sorter like below :

private class RowComparer : System.Collections.IComparer
{
    private static int sortOrderModifier = 1;

    public RowComparer(ListSortDirection sortOrder)
    {
        if (sortOrder == ListSortDirection.Descending)
        {
            sortOrderModifier = -1;
        }
        else if (sortOrder == ListSortDirection.Ascending)
        {
            sortOrderModifier = 1;
        }
    }

    public int Compare(object x, object y)
    {
        var DataGridViewRow1 = (DataGridViewRow)x;
        var DataGridViewRow2 = (DataGridViewRow)y;

        // Try to sort based on the Last Name column.
        int CompareResult = int.Parse(
            DataGridViewRow1.Cells[2].Value.ToString()
            ).CompareTo(int.Parse(
            DataGridViewRow2.Cells[2].Value.ToString()));       
        return CompareResult * sortOrderModifier;
    }
}

And then use it like :

dataGridView1.Sort(new RowComparer(ListSortDirection.Ascending));

You can extend the row compare by accepting more parameters and implement it more generic way. Above example is derived from here . Check it out for further reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM