1:通过列的值让dataGridView中的复选框选中
一个简单而实用的方法:
public void SelectNewAddedItem(string strItems)
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
string re_value = dataGridView1.Rows[i].Cells[2].EditedFormattedValue.ToString();
if (re_value == strItems)
{
dataGridView1.Rows[i].Selected = true;
this.dataGridView1["col_Select", i].Value = true;
break;
}
}
}
“col_Select”是复选框所在列的列名。
2:C/S模式下的 DataGridView里的复选框有没有被点击过呢?
程序一运行,DataGridView里的复选框有些被选择了的,如果用户勾掉已经选择的,或者勾上没有选择的。怎么判断呢?
if(dataGridView1.Rows[i].Cells[j].Value!=null && dataGridView1.Rows[i].Cells[j].Value.ToString().trim()!="")
{
if(Convert.ToBoolean(dataGridView1.Rows[i].Cells[j].Value)==true)
{
MessageBox.Show("selected");
}
else
{
MessageBox.Show("not selected");
}
}
else
{
MessageBox.Show("not selected");
}
for (int i = 0; i < this.dataGridView1.RowCount; i++)
{
if(dataGridView1.Rows[i].Cells[0].Value = true)
{
MessageBox.Show("选中的");
}
else
{
MessageBox.Show("没有选中的");
}
}