如何从datagridview行列表中删除特定索引

时间:2019-02-13 04:54:03

标签: c# list datagridview

如何删除特定索引c#处的datagridview列表

  

列表selectedRows =(从dataGridView2.Rows.Cast()中的行                                                    其中Convert.ToBoolean(row.Cells [“ checkBoxColumn”]。Value)== true                                                     选择行)。ToList();

当执行此linq语句时,它将在selectedRows中添加整行,我不想在List selectedRows列表中添加coulmn 0和1,所以我将如何实现呢?

2 个答案:

答案 0 :(得分:1)

您必须提供您要在select new中选择的列的列表,如下所示,该列将解决问题

List selectedRows = (from row in dataGridView2.Rows.Cast() 
where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true 
select new {
   Field1 = row.Field<string>("Field1"),
   Field2 = row.Field<string>("Field2")
}).ToList();

答案 1 :(得分:0)

是它的窗口形式

我使用名为select的datagridview的collection属性创建复选框

我要在更改要检查的复选框的状态时执行以下代码

foreach (DataGridViewRow item in dataGridView1.Rows)
        {
            if ((bool)item.Cells[0].Value == true)
            {
                int n = dataGridView2.Rows.Add();
                dataGridView2.Rows[n].Cells[0].Value = false;
                dataGridView2.Rows[n].Cells[1].Value = item.Cells[1].Value.ToString();
                dataGridView2.Rows[n].Cells[2].Value = item.Cells[2].Value.ToString();
                dataGridView2.Rows[n].Cells[3].Value = item.Cells[3].Value.ToString();
                dataGridView2.Rows[n].Cells[4].Value = item.Cells[4].Value.ToString();
                dataGridView2.Rows[n].Cells[5].Value = item.Cells[5].Value.ToString();
                dataGridView2.Rows[n].Cells[6].Value = item.Cells[6].Value.ToString();
                dataGridView2.Rows[n].Cells[7].Value = item.Cells[7].Value.ToString();
                dataGridView2.Rows[n].Cells[8].Value = item.Cells[8].Value.ToString();
                dataGridView2.Rows[n].Cells[9].Value = item.Cells[9].Value.ToString();
                dataGridView2.Rows[n].Cells[10].Value = item.Cells[10].Value;

                for (int i = 0; i < dataGridView2.Columns.Count; i++)
                    if (dataGridView2.Columns[i] is DataGridViewImageColumn)
                    {
                        ((DataGridViewImageColumn)dataGridView2.Columns[i]).ImageLayout = DataGridViewImageCellLayout.Stretch;
                        break;
                    }
                foreach (DataGridViewRow row in this.dataGridView2.Rows)
                {
                    row.Cells[0].Value = true;
                }

            }
        }

以上代码位于按钮单击事件之间