dataGridView通过checkbox选择数据
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.Columns[e.ColumnIndex].CellType.Name == "DataGridViewCheckBoxCell") { //Console.WriteLine(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); if ((bool)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == false) { dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true; string patientId = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].EditedFormattedValue; if (!listPatientId.Contains(patientId)) { listPatientId.Add(patientId); } } else { dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false; string patientId = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value; if (listPatientId.Contains(patientId)) { listPatientId.Remove(patientId); } } } Console.WriteLine("dataGridView1_CellClick:"); foreach (var item in listPatientId) { Console.WriteLine(item); } }