Search in This Blog

selected check box record from DataGridView c# winform

Suppose you have a DataGridView with the check box. Data are showing and you want to store only those data that's check boxes is selected, so what you will do? In simple you will use the loop which will retrieve all data from DataGridView from first row to last row and in that loop check the condition is which rows’s check box is selected.

It is a good way but not a smart way, imagine that there are 99999999 rows in the DataGridview and only 100 rows are selected.

Smart way is only create a loop which iterate only 100 times not for 99999999 times.

The Smart way is listed below:
1) Assume that we have the DataGridView with records and also have the check box and its name is "selectMe".

2) use listed below code to retrieve only selected checkboxes.

List<DataGridViewRow> list = dataGridView1.Rows.Cast<DataGridViewRow>().Where(eachrow => Convert.ToBoolean(eachrow.Cells["selectMe"].Value) == true).ToList();
foreach (var row in list) {
    MessageBox.Show(row.Cells["emp_id"].Value.ToString());
}
Previous
Next Post »