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个。
我们如何解决?
答案 0 :(得分: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);
}
}
}
}
它的错误检查更加健壮,并且通过引用而不是索引来删除行。
答案 1 :(得分:1)
<强> EDITED 强>
您可以通过SelectedRows属性删除。
确保数据网格上的MultiSelect
属性设置为true。
然后,您可以选择使用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);
}
答案 2 :(得分: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