Reputation: 203
How to remove list of datagridview at specific index c#
List selectedRows = (from row in dataGridView2.Rows.Cast() where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true select row).ToList();
when this linq statement execute it will add whole row in selectedRows, i do not want to add coulmn 0 and 1 in List selectedRows so how i will achieve this ?
Upvotes: 1
Views: 143
Reputation: 203
Yes its a window form
i make checkbox using collection property of datagridview named as Select
i want to execute below code upon changing status of checkbox to 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;
}
}
}
above code is lies between button click event
Upvotes: 0
Reputation: 176886
you have to provide list of column you want to select in select new
as given below that will resolve issue
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();
Upvotes: 1