1

I generate CheckBoxField in GridView dynamically. but in output the CheckBox is disabled.
How to enable CheckBox dynamically.
I know if add a TemplateField in GridView markup my problem is solved but i won't add TemplateField in GridView

ASPX:

 <asp:GridView ID="GridView2" runat="server">
    </asp:GridView>

Code behind:

    DataTable dTable = new DataTable();
    dTable.Columns.Add("c1", typeof(bool));
    DataRow r = dTable.NewRow();
    r[0] = false;        
    dTable.Rows.Add(r);
    r = dTable.NewRow();
    r[0] = true;
    dTable.Rows.Add(r);

    CheckBoxField chkField = new CheckBoxField();
    chkField.DataField = "c1";
    chkField.HeaderText = "CheckBox";
    chkField.ReadOnly = false;
    GridView2.Columns.Add(chkField);
    GridView2.DataSource = dTable;
    GridView2.DataBind();
2
  • It may be GridView is not editable by default. Can you try adding GridView2.AutoGenerateEditButton=true; before data binding? It may add an edit column but still you might get a clue of what is happening... Commented Sep 20, 2013 at 18:01
  • Why are you adding checkboxfield like this. Can you the purpose may be someone can help you find alternate way. Commented Sep 20, 2013 at 18:05

2 Answers 2

2

I put code in the RowDataBound event to enable the checkbox:

protected void Page_Load(object sender, EventArgs e)
{
    this.GridView2.RowDataBound += GridView2_RowDataBound;

    BindGrid();
}

void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells[0].GetType() == typeof(System.Web.UI.WebControls.DataControlFieldCell))
    {
        TableCell tc = e.Row.Cells[0];
        if (tc.Controls.Count > 0)
        {
            CheckBox cb = (CheckBox)tc.Controls[0];
            if (!(cb == null))
            {
                cb.Enabled = true;
            }
        }
    }
}


private void BindGrid()
{
    DataTable dTable = new DataTable();
    dTable.Columns.Add("c1", typeof(bool));
    DataRow r = dTable.NewRow();
    r[0] = false;
    dTable.Rows.Add(r);
    r = dTable.NewRow();
    r[0] = true;
    dTable.Rows.Add(r);

    //CheckBoxField chkField = new CheckBoxField();
    //chkField.DataField = "c1";
    //chkField.HeaderText = "CheckBox";
    //chkField.ReadOnly = false;
    //GridView1.Columns.Add(chkField);
    GridView2.DataSource = dTable;
    GridView2.DataBind();
}

}

Sign up to request clarification or add additional context in comments.

Comments

0

Optimize Decker97 answer:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}
protected void BindGrid()
{
    DataTable dTable = new DataTable();
    dTable.Columns.Add("c1", typeof(bool));
    DataRow r = dTable.NewRow();
    r[0] = false;
    dTable.Rows.Add(r);
    r = dTable.NewRow();
    r[0] = true;
    dTable.Rows.Add(r);
    GridView2.DataSource = dTable;
    GridView2.DataBind();
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        CheckBox cb = (CheckBox)e.Row.Cells[0].Controls[0];
        cb.Enabled = true;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.