0

In my page i am populating a GridView from code behind by setting the source a custom Datatable that i compile from an XML file, in this way i write the columns header and the rows title. And this works fine, so i am adding checkboxes in the cells while adding the columns in this way:

            DataTable dt = new DataTable();
        dt.Columns.Add(" ");
        foreach (XmlNode xns in doc.DocumentElement.ChildNodes[0])
        {
            foreach (XmlNode xn in xns)
            {
                string tagName = xn.Name;

                dt.Rows.Add(tagName);
            }

        } 

        dt.Columns.Add("Mattina Turno 1", typeof(bool)); //this adds the checkbox
        dt.Columns.Add("Mattina Turno 2", typeof(bool)); 
        dt.Columns.Add("Pomeriggio", typeof(bool)); 

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

I am enabling each checkbox in my Gridview RowDataBound with GridViewRowEventArgs as e in this way:

    protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {

            for (int i = 0; i < e.Row.Cells.Count;i++)
            {
                if (e.Row.Cells[i].GetType() == typeof(System.Web.UI.WebControls.DataControlFieldCell))
                {
                    TableCell tc = e.Row.Cells[i];
                    if (tc.Controls.Count > 0)
                    {
                        CheckBox cb = (CheckBox)tc.Controls[0];
                        if (cb != null)
                        {
                            cb.Enabled = true;
                            colonna = ((GridView)sender).HeaderRow.Cells[i].Text;
                            riga = e.Row.Cells[0].Text;
                            cb.CausesValidation = false;
                            cb.ID = riga + " " + colonna;
                            cb.ToolTip = riga + " " + colonna;
                            cb.AutoPostBack = true;
                            cb.CheckedChanged += new EventHandler(Cb_CheckedChanged);
                            cb.Attributes.Add("runat", "server");
                        }
                    }  
                }
            }

    }

But when i try to handle the check event of the checkbox nothing happens. The checkchanged should call Cb_CheckedChanged but nothing happens.

this is Cb_CheckChanged:

        private void Cb_CheckedChanged(object sender, EventArgs e)
    {
        Cliccato.Text = ((CheckBox)sender).ID.ToString();
        System.Diagnostics.Debug.Write(((CheckBox)sender).ToolTip);
    }

The autopostback seems to work because when I click a checkbox the page refresh but it does not handle any event... Please Help me, i really need your help!

Jonathan I
  • 240
  • 1
  • 4
  • 18
  • Possible duplicate of [Checkboxes in DataGridView not firing CellValueChanged event](https://stackoverflow.com/questions/17275166/checkboxes-in-datagridview-not-firing-cellvaluechanged-event) – MatSnow Aug 28 '17 at 11:39
  • although it could be the same question, you see they found the right answer? It is a '13 question and i can ensure you it is not the same. Please do not mark as duplicated. – Jonathan I Aug 28 '17 at 12:03

2 Answers2

0

You are not setting the runat server property in your code therefore nothing happens when the check box is checked/unchecked.

Maybe you can try something like this:

cb.Attributes.Add("runat", "server");

You should also place a break point inside the if block and check if the code is entering the control initialisation portion.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
aditya
  • 343
  • 2
  • 14
0

Add CheckBox dynamically to GridView like:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    // check if it's not a header and footer
    if (e.Row.RowType == DataControlRowType.Row)
    {
        CheckBox chk = new CheckBox();

        chk.AutoPostBack = true;

        // add checked changed event to checkboxes
        chk.CheckedChanged += new EventHandler(chk_CheckedChanged);

        e.Row.Cells[1].Controls.Add(chk); // add checkbox to second column
    }
}

For getting text from cell of each row use below code in RowDataBound:

if (e.Row.RowType == DataControlRowType.Row)
{
     // assuming there is label in first cell, you cast it that you want
     string cellText = (e.Row.Cells[0].FindControls("Label1") as Label).Text;
}