Data showing a DataGridView in C #

2

How to know if a DataGridView shows the same information every time you execute an SQL query?

 private void timer1_Tick(object sender, EventArgs e)
    {
        data.DataSource = "";
        /* Cargar datos */
        data.DataSource = CCarsData.ConexionDB.CargarTable();
        /* Set Grid */
        setdv();

        comparar();
    }

    private void comparar()
    {
        timer1.Enabled = false;

        foreach (DataGridViewRow Row in data.Rows)
        {
            string Valor = Convert.ToString(Row.Cells[0].Value);
            string status = Convert.ToString(Row.Cells[2].Value);

            foreach(DataGridViewRow row2 in data2.Rows)
            {
                string Valor2 = Convert.ToString(row2.Cells[0].Value);
                string status2 = Convert.ToString(row2.Cells[2].Value);

                if (Valor == Valor2)
                {
                    if (status == "DISPONIBLE" && status2 == "SERVICIO")
                    {
                        MessageBox.Show("El vehiculo " + Row.Cells[0].Value.ToString() + " se encuentra disponible", "Flota", MessageBoxButtons.OK);
                    }
                }
            }               
        }
        data2.DataSource = "";
        /* Cargar datos */
        data2.DataSource = CCarsData.ConexionDB.CargarTableServicio();
        setdv2();
        timer1.Enabled = true;
    }

The grid1 is loaded with the articles table, and the grid2 is loaded with the articles that have service status.

My application is merely informative and does not perform CRUD operations on the database.

***** EDIT ****

I made some changes to the code since in principle it was redundant.

  • In the grid1 charge all articles
  • In grid2 charge only items with "SERVICE" status
  • That is:

     private void Form1_Load(object sender, EventArgs e)
        {
    
            /* Cargar datos */
            data.DataSource = CCarsData.ConexionDB.CargarTable();
            data2.DataSource = CCarsData.ConexionDB.CargarTableServicio();
        }
    
  • When starting the timer just refresh the grid1 and compare with the'grid2'

    private void compare ()     {         timer1.Enabled = false;

        foreach (DataGridViewRow Row in data.Rows)
        {
            string Valor = Convert.ToString(Row.Cells[0].Value);
            string status = Convert.ToString(Row.Cells[2].Value);
    
            foreach(DataGridViewRow row2 in data2.Rows)
            {
                string Valor2 = Convert.ToString(row2.Cells[0].Value);
                string status2 = Convert.ToString(row2.Cells[2].Value);
    
                if (Valor == Valor2)
                {
                    if (status == "DISPONIBLE" && status2 == "SERVICIO")
                    {
                        MessageBox.Show("El vehiculo " + Row.Cells[0].Value.ToString() + " se encuentra disponible", "Flota", MessageBoxButtons.OK);
                    }
                }
            }               
        }
        data2.DataSource = "";
        /* Cargar datos */
        data2.DataSource = CCarsData.ConexionDB.CargarTableServicio();
        setdv2();
        timer1.Enabled = true;
    }
    
  • With this I only make a comparison between all the articles and only those that are with service status

        
    asked by Jose Luis Tovar Fernandez 14.10.2016 в 22:19
    source

    1 answer

    2

    Would not it be easier to manage a field in your modification date / status table so as not to compare the entire table?

    If you want to compare the tables in the same way (I see you only compared two columns), I'll give you an example with Linq.

        private void Comparar()
        {
            var dataSource1  =CCarsData.ConexionDB.CargarTable();
            var dataSource2 = data.DataSource;
    
            var iguales =
                dataSource2.AsEnumerable()
                    .Join(dataSource1.AsEnumerable(), table1 => table1.Field<string>(0),
                        table2 => table2.Field<string>(0),
                        (table1, table2) => new { table1, table2 })
                    .Where(
                        @t =>
                            @t.table1.Field<string>(0).Equals(@t.table2.Field<string>(0)) &&
                            @t.table1.Field<string>(2).Equals(@t.table2.Field<string>(2)))
                    .Select(@t => @t.table1);
            var distintos = from table1 in dataSource2.AsEnumerable()
                            where !iguales.Contains(table1)
                            select table1;
            if (distintos.Any())
            {
                foreach(var distinto in distintos)
                {
                    MessageBox.Show("El vehiculo " + distinto[0] + " se encuentra disponible", "Flota", MessageBoxButtons.OK);
    
                }
            }
        data.DataSource=dataSource1;
        }
    

    Maybe with this your performance improves.

    Greetings

        
    answered by 14.10.2016 / 23:47
    source