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

C# 如何从datagridview行列表中删除特定索引,c#,list,datagridview,C#,List,Datagridview,如何删除特定索引c处的datagridview列表 列出dataGridView2.Rows.Cast中的selectedRows=行 其中Convert.ToBooleanrow.Cells[checkBoxColumn].Value==true 选择row.ToList 当执行此linq语句时,它将在selectedRows中添加整行,我不想在List selectedRows中添加coulmn 0和1,所以我将如何实现这一点?您必须提供要在select new中选择的列的列表,如下所示,这

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

列出dataGridView2.Rows.Cast中的selectedRows=行 其中Convert.ToBooleanrow.Cells[checkBoxColumn].Value==true 选择row.ToList


当执行此linq语句时,它将在selectedRows中添加整行,我不想在List selectedRows中添加coulmn 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();
是的,这是一张窗口表格

我使用名为Select的datagridview的集合属性设置复选框

我想在将复选框的状态更改为check时执行以下代码

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;
                }

            }
        }

上面的代码介于按钮单击事件之间

对您有效吗?是的,但我遇到了另一个问题我想在签入复选框OK时单击按钮事件,但如果我的回答帮助您,请向上投票或选择“接受”,您需要进一步帮助,请更新问题请指导我关于此问题我想在签入复选框时单击按钮事件–如果您想在复选框上调用按钮事件,请单击,您应该将同一个按钮单击事件与复选框绑定,或者您可以从复选框更改/单击事件调用按钮单击事件