用于在数据网格视图上选中复选框的解决方案
本文关键字:复选框 解决方案 视图 数据 数据网 网格 用于 | 更新日期: 2023-09-27 17:57:12
>我在向数据网格视图插入复选框列时使用以下代码,
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Add(checkBoxColumn);
当我尝试选中复选框时,它不是选择ECD(未选中或未选中),有没有更好的方法可以做到这一点?
您只是在网格视图中添加一列,但在分配数据源时,网格视图。将网格视图与数据源绑定以访问行以选中或取消选中复选框。
根据评论进行编辑。
可以使用 RowDataBound 事件在 bihnding 期间使用网格视图的行实例和数据源数据来启用/禁用复选框。
void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("chkBoxID");
if(DataBinder.Eval(e.Row.DataItem, "datasourceColumnName").ToString() == "someval")
chk.Enabled = false;
}
}
您可以使用本文
rotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Check for the row type, which should be data row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the check boxes and assign the values from the data source
((CheckBox)e.Row.FindControl("chkSelect")).Checked = Convert.ToBoolean(((DataRowView)e.Row.DataItem)[1]);
((CheckBox)e.Row.FindControl("chkAdd")).Checked = Convert.ToBoolean(((DataRowView)e.Row.DataItem)[2]);
((CheckBox)e.Row.FindControl("chkEdit")).Checked = Convert.ToBoolean(((DataRowView)e.Row.DataItem)[3]);
((CheckBox)e.Row.FindControl("chkAll")).Checked = Convert.ToBoolean(((DataRowView)e.Row.DataItem)[4]);
//Find the checkboxes and assign the javascript function which should
//be called when the user clicks the checkboxes.
((CheckBox)e.Row.FindControl("chkSelect")).Attributes.Add("onclick", "checkBoxClicked('" +
((CheckBox)e.Row.FindControl("chkSelect")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAdd")).ClientID
+ "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "'," + "'SELECT')");
((CheckBox)e.Row.FindControl("chkAdd")).Attributes.Add("onclick", "checkBoxClicked('" +
((CheckBox)e.Row.FindControl("chkSelect")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAdd")).ClientID
+ "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "'," + "'ADD')");
((CheckBox)e.Row.FindControl("chkEdit")).Attributes.Add("onclick", "checkBoxClicked('" +
((CheckBox)e.Row.FindControl("chkSelect")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAdd")).ClientID
+ "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "'," + "'EDIT')");
((CheckBox)e.Row.FindControl("chkAll")).Attributes.Add("onclick", "checkBoxClicked('" +
((CheckBox)e.Row.FindControl("chkSelect")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAdd")).ClientID
+ "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "'," + "'ALL')");
}
}