Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i try to delete last row on Datagridview and XML database i cant delete. I can delete all of items normal but last row cant be deleted.

here delete button:
C#
private void silbtn2_Click(object sender, EventArgs e)
    {
        XDocument x = XDocument.Load(@"../../Data/gemi1/gemi1s2.xml");
        x.Root.Elements().Where(a => a.Element("id").Value == siltxt2.Text).Remove();
        x.Save(@"../../Data/gemi1/gemi1s2.xml");
        yukle();
    }


Yukle:

C#
void yukle() // Refresh Page
        {
            DataSet dataSet = new DataSet();
            XmlReader xmlFile;
            xmlFile = XmlReader.Create(@"../../Data/gemi1/gemi1s1.xml", new XmlReaderSettings());
            dataSet.ReadXml(@"../../Data/gemi1/gemi1s1.xml");
            if (dataSet.Tables.Count <= 0)
            {
                DataSet dataSetbos = new DataSet();
                dataSetbos.ReadXml(@"../../Data/gemi1/gemi1s1bos.xml");
                dataGridView1.DataSource = dataSetbos.Tables[0];
            }
            else
            {
                dataGridView1.DataSource = dataSet.Tables[0];
            }
            dataGridView1.DataSource = dataSet.Tables[0];
            xmlFile.Close();


What I have tried:

i was tried lot of things but i deleted everything about it.
Posted
Updated 9-Dec-21 0:06am
v2
Comments
Dave Kreskowiak 9-Dec-21 8:03am    
XML is not a database. It's just a markup spec that lets you format data for interchange with other systems.

1 solution

0) Instead of providing the filename like you're doing, create a string constant set to the filename, and then use the filename constant. Doing this will make it possible to change the filename in one place and not worry about whether or not you missed a place that required the change.

C#
const string __FILENAME__ = @"../../Data/gemi1/gemi1s1.xml";

private void silbtn2_Click(object sender, EventArgs e)
{
    XDocument x = XDocument.Load(__FILENAME__);
    x.Root.Elements().Where(a => a.Element("id").Value == siltxt2.Text).Remove();
    x.Save(__FILENAME__);
    yukle();
}


1) Don't bind your datagrid control ItemsSource property to a DataTable. Instead, create an viewmodel entity (inheriting from INotifyPropertyChanged), and put those entities into an ObservableCollection. Finally, bind your datagrid's ItemsSource property to the collection. When you do this, all you have to worry about is updating the collection, and the UI will change automatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900