【问题标题】:How to control the order of selected items from DataGridView using check-boxes?如何使用复选框控制 DataGridView 中选定项目的顺序?
【发布时间】:2021-05-14 23:50:16
【问题描述】:

目标

目前正在寻找一种方法,如何从 DataGridView 中选择项目并按顶部选择的第一个对其进行排序。

示例

用户第一次选择:

Selected  Column1 Column2
             a       1
             b       2
             c       3
   x         d       4

第二次选择...

Selected  Column1 Column2
             a       1
   x         b       2
             c       3
   x         d       4

第三次选择...

Selected  Column1 Column2
   x          a      1
   x          b      2
              c      3
   x          d      4

订购

4th row 
2nd row
1st row

总结

用户选择的第一个项目是第 4 行,然后是第 2 行,最后是第 1 行。

问题

如何按上述顺序获取所有行的列表?

当前代码

我像这样创建了一个复选框列,因此用户可以看到他们选择的内容。

DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.Name = "Selected";
checkBoxColumn.HeaderText = "Selected";
checkBoxColumn.ReadOnly = false;
productsView.Columns.Add(checkBoxColumn);

然后用户选中复选框以标记他们想要的选定记录,然后他们单击一个按钮以打开另一个表单。

但顺序参差不齐。

private void addBtn_Click(object sender, EventArgs e)
{

    foreach (var row in checkedRows)
    {
        DateTime dateTime = dateText.Value;
        string orderNumber = orderNumberText.Text;
    
        SelectedProducts form = new SelectedProducts(dateTime, orderNumber);
        form.ShowDialog();
    }

}

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    我想我会:

    1. 创建一个新列表,根据新订单存储我的数据。

      List<Item> SelectedItemList= new List<Item>
      
    2. 监听复选框列值的变化。选中复选框时添加到“SelectedList”或删除它 未选中。(How to detect DataGridView CheckBox event change?)

      private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
          dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
      }
      
      private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
      {
          if (dataGridView1.Rows.Count > 0)
          {
              if ((bool)dataGridView1.SelectedRows[0].Cells[2].Value) //the index of checkbox column
                 SelectedItemList.Add(itemBindingSource.current as Item)//add data to the list
              else
                 SelectedItemList.Remove(itemBindingSource.current as Item) // remove data to list
          }
      
      }
      
    3. 然后使用列表作为表单显示的参数。

      SelectedProducts form = new SelectedProducts(SelectedItemList);
      form.ShowDialog();
      

    【讨论】:

    • 只需阅读所有这些,一切都有意义,直到到达itemBindingSource.current as Item 这是一个有效的代码吗?
    • 您不需要这样的解决方法,只需根据索引对所选行进行排序即可。
    • @LV98 itemBindingSource.current as Item 只是获取当前选定行并从SelectedItemList 中添加/删除它的一种方式。这个概念是您获取当前选定的行并将其添加/删除到SelectedItemList(或者如果您愿意,可以使用数组)。
    【解决方案2】:

    您不需要通过每次检查更改来跟踪选择。只需在需要时获取选中的行:

    var checkedRows = dataGridView1.SelectedRows.Cast<DataGridViewRow>()
        .Where(r => (bool)r.Cells["Selected"].Value == true)
        .ToList();
    

    【讨论】:

    • 一般来说,你不应该对选择的顺序有任何依赖,无论如何,上面的代码会为你解决问题,
    • 这么简单啊哈! :D 只是做了很多改动。
    • 另一个答案中的建议会起作用,但你真的不需要这样的解决方法:)
    • 我对它进行了一些更改,因为我发现您不依赖于 SelctedRows,而是您拥有自己的复选框列。你甚至不需要订购。只需停止跟踪选择,而是在需要时获取选中的行。
    • 查询结果是List&lt;DataGridViewRow&gt;,对于每一行,您可以使用其Cells 属性。在调用ToList 之前,您还可以选择投影结果,例如:.Select(r=&gt;(THE COLUMN TYPE)r.Cells["THE COLUMN NAME"].Value)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多