1

我正在编写一个程序来管理我们公司的所有客户。我有一个DataGridView每个客户都应该展示的地方。我DataGridView得到了一些文本框,用于显示每个客户的详细信息并添加新客户。我有一个按钮“添加客户”。因此,如果我在文本框中输入一些随机文本并单击“添加客户”,它应该将新客户添加到DataGridView. 如果我重新启动程序,仍然应该保存每个客户。所以我将每个客户的详细信息保存到一个 .xml 文件中。

有人可以帮助我或提示我如何DataGridView通过单击按钮添加客户吗?我得到了保存到 xml 文件的代码:

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();
        }
    }

“添加客户”按钮称为“btnAddKunde”。

提前致谢。

干杯

4

4 回答 4

2

在最后的按钮单击事件中添加这些代码行:

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

但是,您的 XmlTextWriter 版本将覆盖 xml 文件。因此,当您单击按钮时,您只会看到最新添加的行。相反,您可以在按钮单击事件中使用以下代码:

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;
    }
于 2013-09-22T20:26:41.607 回答
1

您需要使用 XMLDocument 类从 xml 文件中检索数据

做这样的事情:

XDocument xmlDoc = XDocument.Load("People.xml");

xmlDoc.Element("employee").Add(new XElement("Person", new XElement("Name", txtName.Text),
new XElement("City", txtCity.Text), new XElement("Age", txtAge.Text)));
于 2013-09-22T19:37:53.590 回答
0

您是在寻找从 xml 检索数据的示例还是只是添加从 xml 检索的字符串数组?

这是添加字符串数组的方法..

string[] s = new string[4];
s[0] = "Salim";
s[1] = "9388938813"
s[2] = "s/10 santhi Nagar, Dpo road, Palakkad"
s[3] = "Kerala"
datagridView1.Rows.Add(s);
于 2013-09-22T19:55:32.257 回答
0
dataGridKunden.Rows.Add(new string[] {surname.text, forename.text, address.text .... });
// add as much as you want.
于 2013-09-22T21:05:24.030 回答