在C#中基于CheckBox进行排序

时间:2018-11-22 15:05:20

标签: c# winforms

我有一个DataGridView,其中有多个列,有一个复选框Column像这样:

enter image description here

我想根据“权重”列以降序对我的datagridview进行排序。但是,如果选中了重量旁边的复选框,则该行应位于列表底部。最后,我想根据权重列进行排序(降序排列)datagridview,我首先看到未选中的行,然后看到选中的行。请帮助我。

1 个答案:

答案 0 :(得分:0)

尝试

 List<DataGridViewRow> newRowcheck = new List<DataGridViewRow>();
 List<DataGridViewRow> newRowUncheck = new List<DataGridViewRow>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[1].Value) == false)
                {
                    newRowUncheck.Add(row);
                }
                else
                {
                    newRowcheck.Add(row);
                }


            }


            dataGridView1.Rows.Clear();


            int RowIndex;

            for (RowIndex = 0; RowIndex < newRowUncheck.Count; RowIndex++)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[RowIndex].Cells[0].Value = newRowUncheck[RowIndex].Cells[0].Value;
                dataGridView1.Rows[RowIndex].Cells[1].Value = newRowUncheck[RowIndex].Cells[1].Value;

            }


            dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);

            DataGridView tempDataGridView = new DataGridView();
            tempDataGridView.Columns.Add("Column1", "Column1 Name in Text");
            tempDataGridView.Columns.Add("nColumn2", "Column2 Name in Text");

            for (int i = 0; i < newRowcheck.Count; i++)
            {
                tempDataGridView.Rows.Add();
                tempDataGridView.Rows[i].Cells[0].Value = newRowcheck[i].Cells[0].Value;
                tempDataGridView.Rows[i].Cells[1].Value = newRowcheck[i].Cells[1].Value;
            }

            tempDataGridView.Sort(tempDataGridView.Columns[0], ListSortDirection.Descending);      

            for (int i = 0; i < tempDataGridView.Rows.Count; i++)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i + RowIndex - 1].Cells[0].Value = tempDataGridView.Rows[i].Cells[0].Value;
                dataGridView1.Rows[i + RowIndex - 1].Cells[1].Value = tempDataGridView.Rows[i].Cells[1].Value;
            }

enter image description here

相关问题