//获取dataGridView的列名及某列是否可编辑及改变某列的颜色
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
string szColumnName = dataGridView1.Columns[i].HeaderText; //列名
dataGridView1.Columns[szColumnName].ReadOnly = true; //是否可编辑
dataGridView1.Columns[i].DefaultCellStyle.BackColor = Color.Blue;//改变颜色
}
//复选框选中
private void DataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//不是序号列和标题列时才执行
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)
{//checkbox 勾上
if ((bool)dgvStaff.Rows[e.RowIndex].Cells[0].EditedFormattedValue == true)
{
dgv.Rows[e.RowIndex].Cells[0].Value = false;//选中改为不选中
}
else
{
dgv.Rows[e.RowIndex].Cells[0].Value = true;//不选中改为选中
}
}
}
}
//复选框选中
private void dgvCall_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
for (int i = 0; i < dgvCall.SelectedRows.Count; i++)
{
dgvCall.SelectedRows[i].Cells["colFullSelected"].Value = !(bool)(dgvCall.SelectedRows[i].Cells["colFullSelected"].Value);
}
}
//选中行变色
private void dgvCall_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//dgvCall.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.FromArgb(180, 255, 255);
dgvCall.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.FromArgb(233, 245, 250);
}