2017-08-28 34 views
1

在我的页面我填充GridView从后面的代码通过设置源自定义Datatable,我从一个XML文件编译,这样我写列标题和行标题。 这工作得很好,所以我在细胞中添加复选框,而以这种方式加入列:从数据表复选框不处理选中的事件

  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(); 

我能够在我的GridView的RowDataBound每个复选框与GridViewRowEventArgs为e这样:

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"); 
         } 
        } 
       } 
      } 

    } 

但是,当我尝试处理复选框的检查事件没有任何反应。 checkchanged应该调用Cb_CheckedChanged但没有任何反应。

这Cb_CheckChanged:

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

将AutoPostBack似乎工作,因为当我点击复选框的页面刷新,但它不处理任何事件...... 请帮助我,我真的需要你的帮助!

+0

可能的复制的(https://stackoverflow.com/questions/17275166/checkboxes-in-datagridview-not-firing-cellvaluechanged-event) – MatSnow

+0

虽然它可能是同一个问题,你看他们找到了正确的答案?这是一个'13问题,我可以确保它不一样。请不要标记为重复。 –

回答

0

动态地GridView控件像添加复选框:

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 
    } 
} 

对于RowDataBound获取文本从下面的代码的每一行使用的细胞:在DataGridView的复选框没有触发CellValueChanged事件]

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; 
} 
0

您没有在代码中设置runat服务器属性,因此当复选框处于选中状态时,没有任何反应。

也许你可以尝试这样的事:

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

你也应该放置一个破发点,如果块内,并检查代码是否进入控制初始化部分。