如何检查<asp:CheckBoxField/>
中是否与数据库中的bool列绑定的单元格是否被检查(即是真还是假)?
例如某些功能,如
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[myCheckBoxFieldIndex].IsChecked()) {do my stuff}
}
会做得很完美。
我只需要访问单元格中的值。
提前致谢!
答案 0 :(得分:0)
首先将控件转换为CheckBox
var cell = e.Row.Cells[myCheckBoxFieldIndex];
var checkbox = cell.Controls.OfType<CheckBox>().FirstOrDefault();
然后使用Checked
属性
if (checkbox != null && checkbox.Checked) { /* Do stuff */ }
答案 1 :(得分:0)
e.Row.Cells[myCheckBoxFieldIndex]
是DataGridViewCell的类型。
您应该将其强制转换为DataGridViewCheckBoxCell
var checkbox = e.Row.Cells[myCheckBoxFieldIndex] as DataGridViewCheckBoxCell;
if (checkbox != null && checkbox.Checked)
{
// your code
}
答案 2 :(得分:0)
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ( ((CheckBox )e.Row .Cells [0].Controls [0]).Checked ==true )
{
//do your stuff
}
}
}