我想通过选中行等到该行的UserID(UserID 为 int型,chkSelect 为DataGridViewCheckBoxColumn)
我的做法:
//绑定到UserID
chkSelect.DataPropertyName = "UserID";
//初始化chkSelect,刚显示出来时都未选中
for (int i = 0; i < dtvUser.Rows.Count-1; i++)
dtvUser.Rows[i].Cells["UserID"].Value = false;
---------------------------------------------------------------------
//循环判断得到选中的行UserID
for (int i = 0; i < dtvUser.Rows.Count - 1; i++)
if ((bool)dtvUser.Rows[i].Cells["UserID"].Value == true) //这里报错->指定的转换无效。求改正,或给出别的方法及代码~!
我的做法:
//绑定到UserID
chkSelect.DataPropertyName = "UserID";
//初始化chkSelect,刚显示出来时都未选中
for (int i = 0; i < dtvUser.Rows.Count-1; i++)
dtvUser.Rows[i].Cells["UserID"].Value = false;
---------------------------------------------------------------------
//循环判断得到选中的行UserID
for (int i = 0; i < dtvUser.Rows.Count - 1; i++)
if ((bool)dtvUser.Rows[i].Cells["UserID"].Value == true) //这里报错->指定的转换无效。求改正,或给出别的方法及代码~!
try {
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0];
if (Convert.ToBoolean(cbx.FormattedValue)) {
str += dr.Cells[1].Value;
}
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}