Read information from an xml file using a dataset and visualize it using a DataGridViwe

1

Hello I have a question when reading an xml file and visualize the information in two datagridview. My intention is to read a file that contains information about several metro lines (class 1) where each subway line has different stations (class 2) by entering that information in a dataset. For this I have implemented the following code:

LinesDataSet.ReadXml(filePath);
dataGridView1.DataSource = LinesDataSet.Tables["CLinia"]; 

In this way I have a datagridview with the information of the lines (where each column is an attribute of that class and each row is a different line).

Later I want that when clicking on a row of this datagridview the stations corresponding to the selected line in another datagridview are displayed. For this I have implemented the following code:

dataGridView2.DataSource = LinesDataSet.Tables["CEstacio"];

The problem I have is that when clicking, all the stations are displayed (of all the lines) and I only want the stations of the selected line to be displayed. How could I manage the dataset to achieve that? I do not know how to access the stations of only one line. The format of the xml file is as follows:

<ArrayOfCLinia xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CLinia>
    <estacions>
      <CEstacio>
        <CodiEstacio>e1</CodiEstacio>
        <Nom>Fondo</Nom>
        <Posicio>
          <double>21.33</double>
          <double>33.43</double>
        </Posicio>
        <Tipus>baixador</Tipus>
        <Ordre>1</Ordre>
      </CEstacio>
      <CEstacio>
        <CodiEstacio>e2</CodiEstacio>
        <Nom>Fabra i Puig</Nom>
        <Posicio>
          <double>21.33</double>
          <double>33.43</double>
        </Posicio>
        <Tipus>baixador</Tipus>
        <Ordre>2</Ordre>
      </CEstacio>
    </estacions>
    <Codi>L1</Codi>
    <Descripcio>Fondo-Hospital</Descripcio>
    <Acronim>linia 1</Acronim>
  </CLinia>
  <CLinia>
    <estacions>
      <CEstacio>
        <CodiEstacio>e1</CodiEstacio>
        <Nom>Hospital</Nom>
        <Posicio>
          <double>21.33</double>
          <double>33.43</double>
        </Posicio>
        <Tipus>baixador</Tipus>
        <Ordre>1</Ordre>
      </CEstacio>
      <CEstacio>
        <CodiEstacio>e2</CodiEstacio>
        <Nom>Sagrera</Nom>
        <Posicio>
          <double>21.33</double>
          <double>33.43</double>
        </Posicio>
        <Tipus>estacio</Tipus>
        <Ordre>2</Ordre>
      </CEstacio>
    </estacions>
    <Codi>L2</Codi>
    <Descripcio>Sagrera-Navas</Descripcio>
    <Acronim>linia 2</Acronim>
  </CLinia>
</ArrayOfCLinia>

Thank you very much in advance.

    
asked by Xim123 22.01.2018 в 11:41
source

1 answer

0

The method ReadXml of DataSet creates some tables of indices that you have to consult in your case to link the lines with the stops. The following code makes use of LINQ and goes step by step obtaining the necessary data. First you get the code of the line (L1, L2 ..). Subsequently, the Id assigned to that line is consulted (0,1 ...) Then, we must go to another table to see the id assigned to the stations of a line, and finally with this data we can filter the stations per line. It would be something like this:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count > 0)
    {
        string linea = dataGridView1.SelectedRows[0].Cells["Codi"].Value.ToString();
        var codigo_linea = LinesDataSet.Tables["CLinia"].AsEnumerable().Where(x => x.Field<string>("Codi") == linea).FirstOrDefault().Field<int>("Clinia_id");
        var id_estaciones = LinesDataSet.Tables["estacions"].AsEnumerable().Where(x => x.Field<int>("Clinia_Id") == codigo_linea).Select(y => y.Field<int>("estacions_id")).FirstOrDefault();
        var estacionesDeLinea = LinesDataSet.Tables["CEstacio"].AsEnumerable().Where(x => x.Field<int>("estacions_id") == id_estaciones).AsDataView();
        dataGridView2.DataSource = estacionesDeLinea;
    }
}

Another option is to load all station data in dataGridView2 and filter depending on the selected line:

LinesDataSet.ReadXml(filePath);
dataGridView2.DataSource = LinesDataSet.Tables["CEstacio"];
dataGridView1.DataSource = LinesDataSet.Tables["CLinia"]; 

....

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count > 0)
    {
        string linea = dataGridView1.SelectedRows[0].Cells["Codi"].Value.ToString();
        var codigo_linea = LinesDataSet.Tables["CLinia"].AsEnumerable().Where(x => x.Field<string>("Codi") == linea).FirstOrDefault().Field<int>("Clinia_id");
        var id_estaciones = LinesDataSet.Tables["estacions"].AsEnumerable().Where(x => x.Field<int>("Clinia_Id") == codigo_linea).Select(y => y.Field<int>("estacions_id")).FirstOrDefault();

        ((DataTable)dataGridView2.DataSource).DefaultView.RowFilter= string.Format("estacions_id = {0}", id_estaciones);
    }
}
    
answered by 22.01.2018 в 13:18