Good day! Trying to select the value of the XML file corresponding to the value in the DataGridView. It turned out to put the value in the DataGrid and select a value, BUT the choice is made regardless of the row, that is, the value will be one regardless of which line to click.

Class:

[Serializable] [XmlRoot(ElementName = ("Document"))] public class Document { [XmlElement(ElementName = "Personal")] public Personal Personal { get; set; } } public class Personal { [XmlElement(ElementName = "Work")] public Work work { get; set; } [XmlAttribute(AttributeName = "info")] public string info { get; set; } } public class Work { [XmlElement(ElementName = "users")] public List<Users> users { get; set; } [XmlAttribute(AttributeName = "info")] public string info { get; set; } } public class Users { [XmlAttribute(AttributeName = "info")] public string info { get; set; } [XmlElement(ElementName = "user")] public List<User> user { get; set; } } public class User { [XmlAttribute(AttributeName = "name")] public string name { get; set; } [XmlElement(ElementName = "company")] public string company { get; set; } [XmlElement(ElementName = "age")] public string age { get; set; } } 

Code:

 public static XmlSerializer format = new XmlSerializer(typeof(Document)); public static FileStream read = new FileStream(@"F:\XML2.xml", FileMode.Open); Document XmlDes = (Document)format.Deserialize(read); //По нажатию кнопки, заполняем DataGrid private void button1_Click(object sender, EventArgs e) { foreach (var poisk in XmlDes.Personal.work.users) { userBindingSource.DataSource = poisk.user; } } //При нажатии на любую строку в DataGrid получаем значение private void userData_CellContentClick(object sender, DataGridViewCellEventArgs e) { foreach (var poisk in XmlDes.Personal.work.users) { //Вот тут загвоздка, я понимаю как вывести конкретный элемент, но как сделать, так что бы он соответствовал значению строки? CollectionUser.Rows.Add(poisk.user[0].age, poisk.user[0].name); } } 

XML file:

 <?xml version="1.0" encoding="utf-8" ?> <Document> <Personal> <Work info="Работающий"> <users info="Информация о работниках"> <user name="Bill Gates"> <company>Microsoft</company> <age>48</age> </user> <user name="Larry Page"> <company>Google</company> <age>42</age> </user> </users> </Work> </Personal> </Document> 

Example: In the DataGrid, when you click on a button, an attribute (name) and two elements (company & age) appear. The second line displays the values ​​of the second element (Larry).

    3 answers 3

    To keep manual work to a minimum, you can proceed as follows, provided that you have made a data binding through the BindingSource .

    Sign the first DataGridView on the CellClick event (this is more convenient than CellContentClick , since you don’t have to get into the contents of the cell).

     private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { var user = (User)userBindingSource.Current; dataGridView2.DataSource = new User[] { user }; } 

    The Current property points to the currently selected object.

    In order for the second datagrid to display the data, you need to bind a collection to it. To do this, dynamically create an array consisting of a single element.

      It seems to me that the snag here:

        foreach (var poisk in XmlDes.Personal.work.users) { userBindingSource.DataSource = poisk.user; } 

      Why are you moving over each user and assigning a DataSource?

      In this case, it turns out that there is always 1 last user in the DataSource.

      Correctly, userBindingSource.DataSource= XmlDes.Personal.work.users

      • I have two DataGrid-a on the form, with the help of this cycle I fill the first DataGrid with all the elements and when I click on a row, the values ​​in the specific <user> element should be displayed in the second DataGrid-e. - Tibomso
      • @Tibomso, why? If you make userBindingSource.DataSource = XmlDes.Personal.work.users, then everything will be done by itself. - iluxa1810 2:01 pm
      • I tried, did not work) One empty line appeared - Tibomso
      • userBindingSource.DataSource = XmlDes.Personal.work.users [0] .user; , this is how it turns out to be put on the form, but still this is not the problem, but thanks for the advice) - Tibomso

      Thanks to all! Understood.

       poisk.user[e.RowIndex].age, poisk.user[e.RowIndex].name