Order Dictionary <string,object>填充DataGridView的行</string,object>

时间:2012-02-01 21:43:49

标签: c# winforms dictionary datagridview

我正在创建一个winforms应用来创建发票。我有一个dataGridView(DGV),我用字典填充它。问题是我需要手动(不按字母顺序)排序行,因为用户决定在哪里放置发票的概念。问题是如何订购字典项目以重新填充订购的DGV。

由于

2 个答案:

答案 0 :(得分:1)

排序通常应由控件IF完成,您使用的控件支持自定义排序。您正在使用的控件似乎支持自定义排序。

请参阅here

中的SortCompare事件

样品:

private void dataGridView1_SortCompare(object sender,
    DataGridViewSortCompareEventArgs e)
{
    // Try to sort based on the cells in the current column.
    e.SortResult = System.String.Compare(
        e.CellValue1.ToString(), e.CellValue2.ToString());

    // If the cells are equal, sort based on the ID column.
    if (e.SortResult == 0 && e.Column.Name != "ID")
    {
        e.SortResult = System.String.Compare(
            dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
            dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
    }
    e.Handled = true;
}

答案 1 :(得分:0)