我在将复选框列插入datagridview时使用以下代码,
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Add(checkBoxColumn);
当我尝试选中复选框时,它不是selectecd(未选中或未选中), 有更好的方法吗?
答案 0 :(得分:0)
您只是在gridview中添加一列,但在为gridview分配数据源时。使用datasource绑定gridview以访问行以选中或取消选中复选框。
根据评论修改。
您可以使用RowDataBound事件来启用/禁用复选框,这些复选框使用网格视图的行实例和副作用中的数据源数据。
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;
}
}
答案 1 :(得分:0)
你可以使用这个article
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')");
}
}