Nathanial s-c
Nathanial s-c

Reputation: 31

how do i select and update info in an xml file

so i know how to load the the data from the selected gridview in to text boxes but how do i make it so it will update the selected place info and not make a new place in the file and also delete a selected place from file if i want to.

this is how i currently load to the datagrid

ds.ReadXml("Database.xml");

string filter = "";
filter = "userdata" + " LIKE '*" + "u" + "*'";
DataView dv = new DataView(ds.Tables[0]);
dv.RowFilter = filter;
dataGridView1.DataSource = dv;
dataGridView1.Columns[4].Visible = false;

and how i load in to the text boxes depending on which is clicked

if (e.RowIndex >= 0)
{

DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
Nametb.Text = row.Cells["name"].Value.ToString();
Locationtb.Text = row.Cells["Location"].Value.ToString();
Infotb.Text = row.Cells["Info"].Value.ToString();
dayvisitcb.Text = row.Cells["Dayvisited"].Value.ToString();

}

this is my xml

<root>
  <place>
    <Name>home</Name>
    <Location>x-292 z 277</Location>
    <Info>home</Info>
    <Dayvisited>10</Dayvisited>
  </place>
  <place>
    <Name>town</Name>
    <Location>x 990 z-2485</Location>
    <Info>gas station</Info>
    <Dayvisited>12</Dayvisited>
  </place>
  <place>
    <Name>crossing</Name>
    <Location>x 90 z-2998</Location>
    <Info>working stiff</Info>
    <Dayvisited>11</Dayvisited>
  </place>
</root>

this file will contain random names and will contain hundreds of places. but so far i can load places to grid,select a place and the details go in to text boxes for editing but then i can only add it to the list and not just update the selected place or delete it

Upvotes: 0

Views: 249

Answers (1)

rene
rene

Reputation: 42494

You better leverage DataBinding. That will do all the heavy lifting, without the need of any coding yourself. You only have to Read and Write the XML file in your dataset.

First in your project add an new item, DataSet (found in Data category), name it Database. Add a DataTable by right clicking on the desgin surface, choose Add > DataTable. Name your DataTable place. Next add in your table the 4 columns Name, Location, Info and Dayvisited.
In the Properties of the Dataset, empty the field Namespace.
When you're done this will be your result:

dataset

Build the project.

Open your Winform in Design mode.
From the Toolbox drag from the category Data the components BindingSource, BindingNavigator, DataGridView and DataSet on your form.

For the DataSource choose the Typed dataset database you created in the previous step.

Click the bindingSource and set the property DataSource to database. Set DataMember to place.

Click the bindingnavigator. Set the property Bindingsource to binsdingSource1 (assuming you left its default name untouced ).

Click the datagridview. Set the property DataSource to bindingSource1.

Add the Textboxes for each field. For each textbox in the properties open the DataBindings category and for the Text property select bindingSource1 -> Name (repeat for the other properties).

Add a button to your form to Load the dataset. This is the code that goes in its click event:

database2.ReadXml("Database.xml");
this.button2.Enabled = true;
button1.Enabled = false;

Add a button to your form to Save the dataset. This is the code that goes in its click event:

database2.WriteXml("Database.xml");

When you're all done this is what your Project should look like:

project overview

When you run it and click the load button, select records, add a new record with the navigator and click save, this will be your result:

example

Upvotes: 1

Related Questions