2014-02-12 47 views
0

我正在編寫代碼,單擊網格視圖(gvrow)的單擊事件。與下面的代碼如何將檢查行添加到數據表

但我的檢查行應該獨自來到數據表(dt)。如果任何行未檢查後檢查它不應該來到數據表。

我下面所示的代碼添加行,但不檢查一個

protected void chkCall_CheckedChanged(object sender, EventArgs e) 
{ 
    foreach (GridViewRow gvrow in gvDetails.Rows) 
    { 
     CheckBox chk = (CheckBox)gvrow.FindControl("chkCall"); 
     if (chk != null & chk.Checked) 
     { 

      //dt.Rows.Add(); 
      DataRow row = dt.NewRow(); 
      row["shopno"] = gvDetails.Rows[i].Cells[0].Text.ToString(); 
      row["Lineitem"] = gvDetails.Rows[i].Cells[1].Text; 
      row["Suppliername"] = gvDetails.Rows[i].Cells[2].Text; 
      row["Dunsnumber"] = gvDetails.Rows[i].Cells[3].Text; 
      row["AgingDays"] = gvDetails.Rows[i].Cells[4].Text; 
      // row["lastfollowupmail"] = gvDetails.Rows[i].Cells[7].Text; 
      dt.Rows.Add(row); 
      i++; 
     } 

    } 

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

} 

請幫我

回答

1

讓您i++外複選框條件。

protected void chkCall_CheckedChanged(object sender, EventArgs e) 
{ 
    int i=0; 
    foreach (GridViewRow gvrow in gvDetails.Rows) 
    { 
     CheckBox chk = (CheckBox)gvrow.FindControl("chkCall"); 
     if (chk != null & chk.Checked) 
     { 

      //dt.Rows.Add(); 
      DataRow row = dt.NewRow(); 
      row["shopno"] = gvDetails.Rows[i].Cells[0].Text.ToString(); 
      row["Lineitem"] = gvDetails.Rows[i].Cells[1].Text; 
      row["Suppliername"] = gvDetails.Rows[i].Cells[2].Text; 
      row["Dunsnumber"] = gvDetails.Rows[i].Cells[3].Text; 
      row["AgingDays"] = gvDetails.Rows[i].Cells[4].Text; 
      // row["lastfollowupmail"] = gvDetails.Rows[i].Cells[7].Text; 
      dt.Rows.Add(row); 

     } 
     i++; 

    } 

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

} 
0

,而不是你的代碼,你可以試試這個

假設您的複選框列被命名爲ChkBox

DataTable tbl = ((DataTable)GridView1.DataSource).Clone();//Clone structure first 
    var rows = dataGridView1.Rows.OfType<GridViewRow>() 
         .Where(r=>Convert.ToBoolean(r.Cells["ChkBox"].Value)) 
         .Select(r=>((DataRowView)r.DataBoundItem).Row); 
    foreach(var row in rows) 
    tbl.ImportRow(row); 
相關問題