简体   繁体   中英

Delete Multiple row from data grid view

I'm trying to delete multi rows from data grid view in c# I use xml file as a data base here is my code when I trying to delete in the data grid view it's delete it correctly but in the xml file it's delete just the last selected row and the sequence row after it

                var selectedRows = CustomersInformation.SelectedRows
          .OfType<DataGridViewRow>()
          .Where(row => !row.IsNewRow)
          .ToArray();

            foreach (var row in selectedRows)
            {
                XDocument Customersdocument = XDocument.Load(@"customers.xml");
                var DeleteQuery = Customersdocument.Descendants("Customer")
                .Where(del => del.Element("PhoneNumber").Value ==
                CustomersInformation.CurrentRow.Cells[1].Value.ToString());

                DeleteQuery.Remove();

                Customersdocument.Save(@"customers.xml");
                CustomersInformation.Rows.Remove(row);

                CustomersInformation.ClearSelection();

            } 

My Xml file looks like this but with more customers

-<Customers>


-<Customer>

<Name>sara</Name>

<PhoneNumber>7176665</PhoneNumber>

<BirthDate>12/28/2000</BirthDate>

<ExpireDate>2023-03-28T09:15:27.8040881+03:00</ExpireDate>

<PackageId>1</PackageId>

<Balance>8</Balance>

</Customer>
</Customers>

According to my understanding, you should try saving CustomersDocument after the end of the for loop. If the problem is not resolved, try debugging the entire process to determine the exact error.

The main problem is this line:

CustomersInformation.CurrentRow.Cells[1].Value.ToString());

The keyword:

...CurrentRow...

acts differently than what you're trying to loop through all the rows in datagridview.

you're trying to do a "foreach" loop, but getting the values from "currentrow".

if you performs a

CustomersInformation.ClearSelection();

The "currentrow" will become "null".

So, u can do this, load the xml first, before the loop:

XDocument Customersdocument = XDocument.Load(@"customers.xml");

foreach (var row in selectedRows)
{
    // remove row
}

// save the file after the loop finished
Customersdocument.Save(@"customers.xml");

in the loop:

foreach (var row in selectedRows)
{
    // get da phoneNumber from foreach row, not currentrow
    var phoneNumber = row.Cells[1].Value.ToString();

    // not from currentrow
    //var phoneNumber = CurrentRow.Cells[1].Value.ToString();

    // generate delete query
    var DeleteQuery = Customersdocument.Descendants("Customer")
    .Where(del => del.Element("PhoneNumber").Value == phoneNumber);

    // do remove
    DeleteQuery.Remove();

    // remove from teh datagridview
    CustomersInformation.Rows.Remove(row);
}

You can do the following:

XDocument doc = XDocument.Load("customers.xml");
    var q = from node in doc.Descendants("Customer")
            let attr = node.Attribute("PhoneNumber")
            where //your condition here with attr.Value
            select node;
    q.ToList().ForEach(x => x.Remove());
    doc.Save("customers.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM