一、添加复选框
ArrayList arr = new ArrayList(); public string checkboxName = "选择"; void StandLibWin_Load(object sender, EventArgs e) { DataGridViewCheckBoxColumn CheckColunms = new DataGridViewCheckBoxColumn(); CheckColunms.Name = checkboxName; CheckColunms.HeaderText = checkboxName; CheckColunms.Width = 60; CheckColunms.TrueValue = "1"; CheckColunms.FalseValue = "0"; dataGridView1.Columns.Insert(1, CheckColunms); dataGridView1.CellContentClick += DataGridView1_CellContentClick; }
二、判断选中状态(此例添加一个ArrayList用来存储选中行的标识)
void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { if (dataGridView1.Columns[e.ColumnIndex].Name == checkboxName) { arr.Clear(); for (int i = 0; i < this.dataGridView1.Rows.Count; i++) { if ((bool)dataGridView1.Rows[i].Cells[checkboxName].EditedFormattedValue == true) { string vClickGridId = dataGridView1.Rows[i].Cells["标识"].Value.ToString(); arr.Add(vClickGridId); } } } } }
=================完结线==============================