Datagridview基于匹配字符串的排序列

时间:2012-07-03 05:00:20

标签: c# .net winforms sorting datagridview

我有一个datagridview,我希望根据从用户输入的字符串部分对行进行排序。输入的字符串将与特定列中的所有字符串进行比较。例如,如果我将“comp”作为搜索词,程序将尝试将搜索词与第一列上的字符串进行比较,并按照以“comp”开头的降序对行进行排序,例如“compare”, “组合”,“计算机”等。不匹配的其余单词要么单独保留,要么按字母顺序排序(以较容易者为准)。

在LINQ中,我知道你可以应用以下代码来实现你想要的字符串数组:

var sortedWords = words.Where(x => x.Contains("comp"))
                       .OrderByDescending(x => x);

如何在Datagridview中实现相同的功能,因为我需要对行进行排序,而不仅仅是特定列中的项目?

编辑:

以下代码给出了System.InvalidOperationException。 (SetCurrentCellAddressCore被调用两次)

    private void DGVPointCtrl_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        MatchComparer mc = new MatchComparer();
        DGVPointCtrl.Sort(mc); //Error
    }

我可能做错了但我不确定为什么。以下是以编程方式添加行以进行测试的代码:

private void BtnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                DGVPointCtrl.Rows.Clear();
                int mainIndex = CmbMainDevice.SelectedIndex;
                int subIndex = CmbSubDevice.SelectedIndex;
                DDCDAO ddcdao = new DDCDAO(DDCGlobal.ddcEngineIP, ddc.Ip);
                string pointListType;
                object rs;

                //Currently only supports IO DDC Request
                //TO DO: Change DDCDAO to send proper subdevice requests

                if (mainIndex == 0) //IO
                {
                    #region Main Device: IO
                }


                //First row is for searching items
                DGVPointCtrl.Rows.Add(new DataGridViewRow());
                for (int i = 1; i < 5; i++)
                {
                    DGVPointCtrl.Rows.Add(new DataGridViewRow());
                    DGVPointCtrl.Rows[i].ReadOnly = true;
                }
                DGVPointCtrl.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic;
                DGVPointCtrl.Rows[0].DefaultCellStyle.Font =
                new Font(DGVPointCtrl.DefaultCellStyle.Font, FontStyle.Italic | FontStyle.Bold);

                if (subIndex == 1) //BI
                {
                    PointDGVColumnGenerate("IO_BI");

                }
                else if (subIndex == 2) //BO
                {
                    PointDGVColumnGenerate("IO_BO");
                }
                else if (subIndex == 3) //AI
                {
                    PointDGVColumnGenerate("IO_AI");
                }
                else if (subIndex == 4) //AO
                {
                    PointDGVColumnGenerate("IO_AO");
                }

                DGVPointCtrl.Rows[1].Cells[0].Value = "IO12314";
                DGVPointCtrl.Rows[2].Cells[0].Value = "IO21948";
                DGVPointCtrl.Rows[3].Cells[0].Value = "IO28194";
                DGVPointCtrl.Rows[4].Cells[0].Value = "VP12984";
                DGVPointCtrl.Rows[2].Cells[1].Value = "asdf";

                #endregion
            }
            catch
            {
            }
      }

    private void PointDGVColumnGenerate(string key)
    {
        int colCount = 0;
        DGVColumnTable.Clear();

        for (int i = 0; i < COL_MAX; i++)
        {
            DGVPointCtrl.Columns[i].HeaderText = "   ";
            DGVPointCtrl.Columns[i].Visible = true;
        }

        foreach (string s in UIConstant.DDCPCtrlListColumnText[key])
        {
            DGVPointCtrl.Columns[colCount].HeaderText = s;
            DGVColumnTable.Add(DGVPointCtrl.Columns[colCount]);
            colCount++;
        }
    }

EDIT2:

public class MatchComparer : IComparer
{
    private static IComparer defaultComparer = new CaseInsensitiveComparer();
    int IComparer.Compare(object x, object y)
    {
        DataGridViewRow xr = (DataGridViewRow)x;
        DataGridViewRow yr = (DataGridViewRow)y;

        string xs = "";
        string ys = "";
        try
        {
            xs = xr.Cells[0].Value.ToString();
        }
        catch
        {
        }
        try
        {
            ys = yr.Cells[0].Value.ToString();
        }
        catch
        {
        }

        if (HasMatch(xs) && !HasMatch(ys)) return -1;
        else if (!HasMatch(xs) && HasMatch(ys)) return 1;
        else return defaultComparer.Compare(xs, ys);
    }

1 个答案:

答案 0 :(得分:1)

只有在您自己填充网格而不是将其绑定到数据库时,才可能这样做。

DataGridViewColumn.SortMode设为Programmatic

使用DataGridView.Sort强加一个这样的比较器:

public class MatchComparer : IComparer  {
     int IComparer.Compare(object x, object y)  {
         if (HasMatch(x) && !HasMatch(y)) return -1;
         else if (!HasMatch(x) && HasMatch(y)) return 1;
         else return defaultComparer.Compare(x, y);
     }

     private bool HasMatch(object x) {
         return x is string && ((string)x).StartsWith("comp");
     }

     private static IComparer defaultComparer = new CaseInsensitiveComparer();
}