Hi People, im new here.

I have a app to register CandidatesForJob.. i have a datagridview and i load data from xml file. I Have a object with data. But my boss sad me to associate each row to my object. Who can help me?

That button , open my AddCandidateForm.

 private void newToolStripMenuItem_Click(object sender, EventArgs e)
            AddCandidate MyForm = new AddCandidate();
            if (MyForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) {

                CandidatesForJob _c = MyForm.resultado;

                XElement xmlNode = new XElement("Candidate",
                  new XElement("FullName", _c.FullName ),
                  new XElement("YearsOfExperience", _c.YearsOfExperience ),
                  new XElement("Job", _c.Job ),
                  new XElement("Certification", _c.Certification ),
                  new XElement("Email", _c.Email ));

                XElement xmlFile;
                    xmlFile = XElement.Load("diogomonteiro.xml");

                catch (XmlException)
                    xmlFile = new XElement("CandidatesForJob", xmlNode);

And my addButton on AddCandidate form have that code:

 private void btnAddList_Click(object sender, EventArgs e)
            _c = new CandidatesForJob();
            _c.FullName = txtFullName.Text;
            _c.YearsOfExperience = txtYearsExperience.Text;
            _c.Job = txtPreviousJob.Text;
            _c.Certification = txtCertification.Text;
            _c.Email = txtEmail.Text;

            if (!File.Exists("diogomonteiro.xml"))
                using (File.Create("diogomonteiro.xml"))
                { }

            if (String.IsNullOrEmpty(txtFullName.Text))
                MessageBox.Show("Enter your Full Name please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (String.IsNullOrEmpty(txtYearsExperience.Text))
                MessageBox.Show("Enter a number in Years Of Experience please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (String.IsNullOrEmpty(txtPreviousJob.Text))
                MessageBox.Show("Enter your previous job please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (String.IsNullOrEmpty(txtCertification.Text))
                MessageBox.Show("Enter your certification please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (String.IsNullOrEmpty(txtEmail.Text))
                MessageBox.Show("Enter your email please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;

Sorry my english :s

3 Years
Discussion Span
Last Post by mdev

But my boss sad me to associate each row to my object.
Is object the datagridview?
What do you mean by associate?
Datagridview has a property called DataSource, is it that what you want?


sorry my english but what i want its how to convert datarow to an Object and than write that objects in my "database" (xml file).


I would suggest that XmlSerializer would be the way to go here.

First make sure that all the properties you want to save are public and that you have the get/set added. Next to make sure that the columns display the headers that you want for each property, add the DisplayName attribute:

public class CandidatesForJob
    public string FullName { get; set; }
    public double YearsOfExperience { get; set; }
    public string Job { get; set; }
    public string Certification { get; set; }
    public string Email { get; set; }
    public CandidatesForJob()
        FullName = "";
        YearsOfExperience = 0.0;
        Job = "";
        Certification = "";
        Email = "";

Now using a list of candidatesforjobs we can use xmlserializer to read and write the list to and from an xml file and display the data in a datagridview:

List<CandidatesForJob> candList = new List<CandidatesForJob>();
candList.AddRange(new CandidatesForJob[]
    new CandidatesForJob(),
    new CandidatesForJob(),
    new CandidatesForJob()
//Create serializer
System.Xml.Serialization.XmlSerializer xSer = new System.Xml.Serialization.XmlSerializer(typeof(List<CandidatesForJob>));
//Write data to file
using (StreamWriter sw = new StreamWriter("xmltest.xml"))
    xSer.Serialize(sw, candList);
//Read data from file
using (StreamReader sr = new StreamReader("xmltest.xml"))
    candList = (List<CandidatesForJob>)xSer.Deserialize(sr);
//Bind list to datagridview
dataGridView1.DataSource = new BindingList<CandidatesForJob>(candList);
//with the list bound like this, any changes to the list are reflected 
//in the datagridview. There should be an extra row of data displayed 
//after this
candList.Add(new CandidatesForJob());

Edited by tinstaafl

Votes + Comments
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.