antowizzy
antowizzy

Reputation: 11

Dynamic checkbox on gridview

I have an c# aspx web application that is meant to generate most of its components on the fly. There is this GridView that is meant to display some check boxes.

The wierd thing is that it does not show the component on run but the namespace, i.e. System.UI.Webcontrols.CheckBox.

I need to see the checkbox; how can this be fixed?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" />
    </div>
    </form>
</body>
</html>

[aspx.cs]

namespace ChkBoxOnGridView
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateGridView();
        }
        protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = new CheckBox();
                e.Row.Cells[1].Controls.Add(cb);
            }
        }

        private void PopulateGridView()
        {
            GridView1.DataSource = null;
            GridView1.Columns.Clear();

            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn("DTA", typeof(System.String)));
            dt.Columns.Add(new DataColumn("Is Visible", typeof(System.Web.UI.WebControls.CheckBox)));

            CheckBox chkbx = new CheckBox();
            chkbx.Checked = true;

            DataRow row1 = dt.NewRow();
            row1["DTA"] = "Some Text";
            row1["Is Visible"] = chkbx;
            dt.Rows.Add(row1);

            foreach (DataColumn col in dt.Columns)
            {
                BoundField bField = new BoundField();
                bField.DataField = col.ColumnName;
                bField.HeaderText = col.ColumnName;
                GridView1.Columns.Add(bField);
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}

Upvotes: 0

Views: 10085

Answers (2)

Jon P
Jon P

Reputation: 19772

Going out on a limb i would say that you can't hold a control in a DataTable. The [Is Visible] should be a boolean value

I'd add columns in the On Init event, using a template column. See this SO question: Adding dynamic columns to an ASP.NET Gridview

Upvotes: 1

JotaBe
JotaBe

Reputation: 39015

You need to create each object individually in each row using the GridView.RowdataBound event.

You can add it using the e.Row.Cells[]

But, to get sure that you're creating it in the right place, you have to check in this event that you are in a row which is not a header or footer, or pager. You do this by checking the GridViewRow.RowType Property of the e.Row and check if it's DataRow

 void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    CheckBox cb = new CheckBox();
    // cb.id = ... and other control setup
    // add your control here:
    e.Row.Cells[0].Controls.Add(cb);
  }
}

Upvotes: 2

Related Questions