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;
                try
                {
                    xmlFile = XElement.Load("diogomonteiro.xml");
                    xmlFile.Add(xmlNode);

                }
                catch (XmlException)
                {
                    xmlFile = new XElement("CandidatesForJob", xmlNode);
                }
                xmlFile.Save("diogomonteiro.xml");

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);
                return;
            }
            if (String.IsNullOrEmpty(txtYearsExperience.Text))
            {
                MessageBox.Show("Enter a number in Years Of Experience please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (String.IsNullOrEmpty(txtPreviousJob.Text))
            {
                MessageBox.Show("Enter your previous job please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (String.IsNullOrEmpty(txtCertification.Text))
            {
                MessageBox.Show("Enter your certification please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (String.IsNullOrEmpty(txtEmail.Text))
            {
                MessageBox.Show("Enter your email please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

Sorry my english :s

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
{
    [DisplayName("Name")]
    public string FullName { get; set; }
    [DisplayName("Experience")]
    public double YearsOfExperience { get; set; }
    [DisplayName("Job")]
    public string Job { get; set; }
    [DisplayName("Cert.")]
    public string Certification { get; set; }
    [DisplayName("Email")]
    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);
}
candList.Clear();
//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 1 Year Ago by tinstaafl

Comments
Nice.
This question has already been answered. Start a new discussion instead.