2013-04-22 51 views
0
List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>(); 
foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell; 
    if (Convert.ToBoolean(chk.Value) == true) 
     rowsToDelete.Add(row); 
} 
//loop through the list to delete rows added to list<T>: 
foreach (DataGridViewRow row in rowsToDelete) 
     dataGridView1.Rows.Remove(row); 

我選擇了3個項目,它刪除了2個,但後面留下了1個。從我的DataGridView中刪除多行後面留下1個

我們該如何解決?

+0

您發佈的代碼工作。我檢查的一切都被刪除。 – LarsTech 2013-04-22 19:00:52

+0

我檢查的最後一項留下。我選擇了3個項目,其中2個被刪除。最後一個被選中但沒有被刪除 – 2013-04-22 19:03:44

+0

...並且我的代碼沒有得到相同的結果。 – LarsTech 2013-04-22 19:06:21

回答

1

EDITED

您可以通過SelectedRows屬性刪除。

確保MultiSelect屬性設置爲true的DataGrid中。

然後,你可以利用你的選擇的情況下SelectedRows屬性:

這是你需要什麼,試試這個代碼:

for (int i = dataGridView1.Rows.Count -1; i >= 0 ; i--) 
{ 
    if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue) 
    { 
     dataGridView1.Rows.RemoveAt(i); 
    } 
} 

OR

foreach (DataGridViewRow row in DataGridView1.SelectedRows) 
{ 
    DataGridView1.Rows.Remove(row); 
} 
+0

SelectedRows是突出顯示的行。我想獲取複選框中有複選標記的行(這是每行的0列) – 2013-04-22 19:28:11

+0

@CocoaDev我編輯了答案:) – Obama 2013-04-22 19:33:32

2

已更新

這聽起來像你想刪除未檢查的項目。

你將不得不做的是從底部開始到頂部,否則你將剩下額外的物品。

例子:

for (int i = DataGridView1.SelectedRows.Count - 1; -1 < i; i--) 
{ 
    object objChecked = DataGridView1.SelectedRows[i].Cells[0].Value; 
    if ((objChecked != null) && !(bool)objChecked) 
    { 
    DataGridView1.Rows.RemoveAt(i); 
    } 
} 

更新2:

基於以下您的意見,這個版本看Rows而不是SelectedRows

檢查Rows開始你的循環之前存在:

if ((0 < DataGridView1.Rows.Count) && (0 < DataGridView1.Columns.Count)) 
{ 
    for (int i = DataGridView1.Rows.Count - 1; -1 < i; i--) 
    { 
    var row = DataGridView1.Rows[i]; 
    if ((row.Cells[0].Value != null) && (row.Cells[0].Value != DBNull.Value)) 
    { 
     bool isChecked = (bool)row.Cells[0].Value; 
     if (isChecked) 
     { 
     DataGridView1.Rows.Remove(row); 
     } 
    } 
    } 
} 

這是一個小更穩健的錯誤檢查,並通過引用而不是由它的索引中刪除行。

+0

我不想要「SelectedRows」。我想要檢查的行(這是每行的第0列)。我想保留具有複選標記的行。 – 2013-04-22 19:28:34

+0

@CocoaDev你想保留具有複選標記的行嗎?你的代碼完全相反。 – LarsTech 2013-04-22 19:35:46

+0

一旦我得到它的工作方式,我可以做相反的事情來得到相反的結果。現在它不適用於複選框。做相反的事情,我可以搜索那些未被選中的複選框。 – 2013-04-22 19:38:32

0

ASPXPAGE:

<strong>Asp.Net : Delete Multiple Records form datagridview in one time<br /> 
     </strong> 

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
      CellPadding="4" EnableModelValidation="True" ForeColor="Black"> 
      <Columns> 
       <asp:TemplateField> 
        <EditItemTemplate> 
         <asp:CheckBox ID="CheckBox1" runat="server" /> 
        </EditItemTemplate> 
        <ItemTemplate> 
         <asp:CheckBox ID="CheckBox1" runat="server" /> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="id" HeaderText="Sr No" /> 
       <asp:BoundField DataField="doc_name" HeaderText="Name" /> 
       <asp:BoundField DataField="doc_add" HeaderText="Address" /> 
       <asp:BoundField DataField="doc_mob" HeaderText="Mobile No" /> 
       <asp:BoundField DataField="doc_email" HeaderText="Email" /> 
      </Columns> 
      <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> 
      <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> 
      <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> 
     </asp:GridView> 
     <br /> 
     <asp:Button ID="Button1" runat="server" Font-Size="12pt" 
      onclick="Button1_Click1" Text="Delete" /> 
     <br /> 

代碼頁背後:

SqlConnection conn = new SqlConnection(@"server=server-pc; database=HMS; integrated security=true"); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (IsPostBack == false) 
     { 
      load_data(); 
     } 
    } 

    public void load_data() 
    { 
     SqlDataAdapter adp = new SqlDataAdapter("select * from doc_master", conn); 
     DataSet ds = new DataSet(); 
     adp.Fill(ds); 
     GridView1.DataSource = ds.Tables[0]; 
     GridView1.DataBind(); 
    } 
    protected void Button1_Click1(object sender, EventArgs e) 
    { 
     CheckBox ch; 
     for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      ch = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1]; 
      if (ch.Checked == true) 
      { 
     int id = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text); 
     SqlCommand cmd = new SqlCommand("delete from doc_master where ID=" + id + " ", conn); 
     conn.Open(); 
     cmd.ExecuteNonQuery(); 
     conn.Close(); 
      } 
     } 

     load_data(); 
    } 

的詳細代碼訪問:: http://www.gtuguide.com/2014/05/deleting-multiple-rows-in-gridview.html