c#中datagridview里checkbox的使用方法
1、属性设置checkboxcolumn
name:cb_check
falsevalue:false
truevalue:true
datagridview中的readonly设置为false.
2、 //单项选择设置
private void dgv_zy_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int count = Convert.ToInt16(dgv_zy.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"]; {
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
checkCell.Value = false;
}
continue;
}
}
3、获取选择的数据
int count = Convert.ToInt32(dgv_zy.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
//如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
dgv_zy.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
//从 DATAGRIDVIEW 中获取数据项
string z_zcode = dgv_zy.Rows[i].Cells[0].Value.ToString().Trim();
//........................................ }
}
1、属性设置checkboxcolumn
name:cb_check
falsevalue:false
truevalue:true
datagridview中的readonly设置为false.
2、 //单项选择设置
private void dgv_zy_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int count = Convert.ToInt16(dgv_zy.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"]; {
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
checkCell.Value = false;
}
continue;
}
}
3、获取选择的数据
int count = Convert.ToInt32(dgv_zy.Rows.Count.ToString());
for (int i = 0; i < count; i++)
{
//如果DataGridView是可编辑的,将数据提交,否则处于编辑状态的行无法取到
dgv_zy.EndEdit();
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv_zy.Rows[i].Cells["cb_check"];
Boolean flag = Convert.ToBoolean(checkCell.Value);
if (flag == true) //查找被选择的数据行
{
//从 DATAGRIDVIEW 中获取数据项
string z_zcode = dgv_zy.Rows[i].Cells[0].Value.ToString().Trim();
//........................................ }
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
You can use any of these statements to get value in the selected cell
textBox1.Text = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
textBox1.Text = (string)dataGridView1.CurrentCell.Value;
textBox1.Text = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
If you want only Id value when you selecet any column in a particular row, you can use, change the values in red font as per your requirement that is column(Id column) you are looking for
textBox1.Text = (string)dataGridView1[columnIndex or columnName, e.RowIndex].Value;
textBox1.Text = (string)dataGridView1.Rows[e.RowIndex].Cells[coulmnIndex or columnName].Value;
}

浙公网安备 33010602011771号