2016-01-22 91 views
1

我怎麼可以添加,刪除和編輯多行 「包含文本框」 來的GridViewGridView的添加/刪除行

沒有插入到DB

我試圖

Gridview1.rows.add(datarow) 

而對於刪除

Gridview1.rows.remove(datarow) 

但未檢測到所選行

+0

你如何構建你的數據行?你能展示更多的代碼嗎? – lyz

+0

您可以綁定到沒有數據庫的'DataTable'。我不會推薦直接操縱網格行,只是操縱它綁定的數據。 – Crowcoder

回答

1

您可以在下面的代碼中使用該方法。在所示的addRow方法中,插入新的網格行。邏輯是我們需要將網格重新綁定到包含原始行和新空行的新數據源。您可以使用類似的方法進行刪除(創建一個刪除了行的新數據源,然後重新綁定網格)。

刪除時使用方法deleteRow。我假設你在網格行中有一個複選框控件,其編號爲chkDelete,選中時表示該行需要刪除。您可以使用deleteRow的方法同時刪除多行。

如果您使用以下兩種方法添加行並刪除行,那麼編輯後的文本框會自動保留其新值,即始終保留爲editing would then be automatically taken care of

作出的假設此外,我假定除了複選框外,網格行中還有3個文本框。因爲有3個文本框,所以在下面的方法中創建的DataTable應該包含這3個文本框的3列,這些列應該是字符串類型。

添加一行

protected void addRow() 
{ 
    DataTable dt = new DataTable(); 
    //add code to create columns for this data table 
    //only create columns for textbox data 
    dt.Columns.Add("Column1", typeof(string)); 
    dt.Columns.Add("Column2", typeof(string)); 
    dt.Columns.Add("Column3", typeof(string)); 
    DataRow dr = null; 

    //build a data source of existing rows 
    foreach (GridViewRow gridRow in grid1.Rows) 
    { 
     dr = dt.NewRow(); 

     //set only text box values in new data source 
     //so checkbox column for row selection will be ignored 
     TextBox txtColumn1 = gridRow.FindControl("txtColumn1") as TextBox; 
     TextBox txtColumn2 = gridRow.FindControl("txtColumn2") as TextBox; 
     TextBox txtColumn3 = gridRow.FindControl("txtColumn3") as TextBox; 

     dr[0] = txtColumn1.Text; 
     dr[1] = txtColumn2.Text; 
     dr[2] = txtColumn3.Text; 

     dt.Rows.Add(dr); 
    } 

    //create the row in data sourec for the new grid row 
    dr = dt.NewRow(); 
    dt.Rows.Add(dr); 

    //bind the grid view, which will now show you the new added row in addition to original rows 
    grd.DataSource = dt; 
    grd.DataBind(); 
} 

刪除行(S)

protected void deleteRow() 
{ 
    DataTable dt = new DataTable(); 
    //add code to create column for this data table 
    //only set column for textbox columns 
    dt.Columns.Add("Column1", typeof(string)); 
    dt.Columns.Add("Column2", typeof(string)); 
    dt.Columns.Add("Column3", typeof(string)); 

    //build a data source of existing rows 
    foreach (GridViewRow gridRow in grid1.Rows) 
    { 
     //get whether the checkbox for deleting row is checked or not 
     CheckBox chkDelete = gridRow.FindControl("chkDelete") as CheckBox; 
     //do not add original row if it was checked to be deleted 
     if(!chkDelete.Checked) 
     { 
      dr = dt.NewRow(); 

      //set only text box values in new data source 
      //so checkbox column for row selection will be ignored 
      TextBox txtColumn1 = gridRow.FindControl("txtColumn1") as TextBox; 
      TextBox txtColumn2 = gridRow.FindControl("txtColumn2") as TextBox; 
      TextBox txtColumn3 = gridRow.FindControl("txtColumn3") as TextBox; 

      dr[0] = txtColumn1.Text; 
      dr[1] = txtColumn2.Text; 
      dr[2] = txtColumn3.Text; 

      dt.Rows.Add(dr); 
     } 
    } 

    //bind the grid view, which will now NOT have the deleted rows 
    grd.DataSource = dt; 
    grd.DataBind(); 
}