删除多行从我的DataGridView留下1
本文关键字:DataGridView 留下 我的 删除 | 更新日期: 2023-09-27 18:10:18
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项。
我们如何修复它?
已更新
听起来你想删除未选中的项目。
你要做的就是从底部开始到顶部,否则你会剩下多余的项目。
的例子:
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);
}
}
}
}
它在错误检查方面更加健壮,并且通过引用而不是通过索引来删除行。
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);
}
}
或
foreach (DataGridViewRow row in DataGridView1.SelectedRows)
{
DataGridView1.Rows.Remove(row);
}
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