Search code examples
c#xmlwinformsdatagridviewform-load

Adding item to DataGridView by clicking button using c#


I'm writing a program for manage all customers we have in our company. I have a DataGridView where every single customer should display. And right to the DataGridView I got some textboxes, for displaying the details of every customer and for add a new customer. And I got a button "Add Customer". So if I type in some random text in textboxes and click on "Add Customer" it should add the new customer to the DataGridView. And if I restart the program, every customer should still be saved. So I save the details of every customer to a .xml file.

Can someone help me or give me a hint how I can add customers to DataGridView by clicking button? I got this code for saving to xml file:

public partial class Form1 : Form
    {
        const string folder = @"C:\Users\Römel\Desktop\Save";
        const string basename = "save.xml";
        string filename = folder + "\\" + basename;

        public Form1()
        {
            InitializeComponent();
            if (Directory.Exists(folder))
            {
                if (File.Exists(filename))
                {
                    DataSet flatDataSet = new DataSet();
                    flatDataSet.ReadXml(filename);
                    DataTable table = flatDataSet.Tables[0];
                    dataGridKunden.DataSource = table;
                }

                dataGridKunden.Columns["KundenNr"].Visible = false;
                dataGridKunden.Columns["Adresse"].Visible = false;
                dataGridKunden.Columns["Ort"].Visible = false;
                dataGridKunden.Columns["Telefon"].Visible = false;
                dataGridKunden.Columns["Mail"].Visible = false;

                dataGridKunden.ScrollBars = ScrollBars.None;
            }
        }

        private void btnAddKunde_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            if (!File.Exists(filename))
            {
                File.Create(filename);
            }
            XmlTextWriter xwriter = new XmlTextWriter(filename, Encoding.Unicode);
            xwriter.WriteStartDocument();
            xwriter.WriteStartElement("Kundenverwaltung");
            xwriter.WriteStartElement("KundenNr");
            xwriter.WriteString(txtKundenNr.Text);
            xwriter.WriteEndElement();
            xwriter.WriteStartElement("Nachname");
            xwriter.WriteString(txtKundeNachname.Text);
            xwriter.WriteEndElement();
            xwriter.WriteStartElement("Vorname");
            xwriter.WriteString(txtKundeVorname.Text);
            xwriter.WriteEndElement();
            xwriter.WriteStartElement("Adresse");
            xwriter.WriteString(txtKundeAdresse.Text);
            xwriter.WriteEndElement();
            xwriter.WriteStartElement("Ort");
            xwriter.WriteString(txtKundeOrt.Text);
            xwriter.WriteEndElement();
            xwriter.WriteStartElement("Telefon");
            xwriter.WriteString(txtKundeTel.Text);
            xwriter.WriteEndElement();
            xwriter.WriteStartElement("Mail");
            xwriter.WriteString(txtKundeMail.Text);
            xwriter.WriteEndElement();
            xwriter.WriteEndDocument();
            xwriter.Close();
        }
    }

The button "Add Customer" is called "btnAddKunde".

Thanks in advance.

Cheers


Solution

  • Add These line of code in your button click event in last:

    if (File.Exists(filename))
    {
          DataSet flatDataSet = new DataSet();
          flatDataSet.ReadXml(filename);
          DataTable table = flatDataSet.Tables[0];
          dataGridKunden.DataSource = table;
    }
    

    However, your version of XmlTextWriter will overwrite the xml file. Thus, when you click on button you will see only the latest added row. Instead you can use below code in your button click event:

    private void btnAddKunde_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            if (!File.Exists(filename))
            {
                using (File.Create(filename))
                {}
            }
    
            XElement xmlNode = new XElement("Kundenverwaltung",
                                            new XElement("KundenNr", txtKundenNr.Text),
                                            new XElement("Nachname", txtKundeNachname.Text),
                                            new XElement("Vorname", txtKundeVorname.Text),
                                            new XElement("Adresse", txtKundeAdresse.Text),
                                            new XElement("Ort", txtKundeOrt.Text),
                                            new XElement("Telefon", txtKundeTel.Text),
                                            new XElement("Mail", txtKundeMail.Text)
                );
            XElement xmlFile;
            try
            {
                xmlFile = XElement.Load(filename);
                xmlFile.Add(xmlNode);
    
            }
            catch (XmlException)
            {
                xmlFile = new XElement("Customers", xmlNode);
            }
            xmlFile.Save(filename);
            DataSet flatDataSet = new DataSet();
            flatDataSet.ReadXml(filename);
            DataTable table = flatDataSet.Tables[0];
            dataGridKunden.DataSource = table;
        }