Copy values from a DataGridView to a new DataTable in order to be compared

0

I have the following conflict, by means of several objects and queries I am consulting 4 different databases that together load information with a DataGrid , this DataGrid in turn must feed To another table with the information it contains, the problem is when I try to compare the content of the grid with a DataTable that queries the final table that should store the information of the grid , Who gives me an idea of how to make such a comparison? Thanks for your valuable help.

/*Variables Globales*/
bool existe = false;
string fechaI = dtpInicial.Value.ToString("yyyy-MM-dd");
string fechaF = dtpFinal.Value.ToString("yyyy-MM-dd");
string estaciones = cmbEstacion.SelectedValue.ToString().Substring(0, cmbEstacion.SelectedValue.ToString().IndexOf('~'));
/*Objetos*/
Datos.Corresponsalia objCorresponsalia = new Datos.Corresponsalia();
DataTable dtCorresponsalias = new DataTable();
dtCorresponsalias = objCorresponsalia.ConsultaCorresponsalia(fechaI, fechaF, estaciones);
if (dtCorresponsalias==dgvRegistros.DataSource)
{
    existe = true;
    if (existe)
    {
        objCorresponsalia.BorrarDuplicados(fechaI, fechaF, estaciones);
    }
    else
    {
            if (dgvRegistros.Rows.Count > 0)
            {
                foreach (DataGridViewRow row in dgvRegistros.Rows)
                {
                    objCorresponsalia.InsertaFilasDataGrid(fechaI, fechaF, estaciones);

                    objValidacion.MostrarAviso("Cargando registros en la Base de Datos por favor espere un Momento", false, lblAviso);
                }
                objValidacion.MostrarAviso("Carga exitosa en la Base de Datos", false, lblAviso);
            }
            else
            {
                objValidacion.MostrarAviso("No se encontraron registros para guardar", true, lblAviso);
            }
        }
    }
}
    
asked by Alberto Arenas 15.12.2016 в 20:04
source

1 answer

1

If the idea is to validate on the datatable you can help with linq, or something like being

bool existe = dtCorresponsalias.Rows.Cast<DataGridViewRow>()
                                .Any(x=> x.Cells["NombreCampo"].Value.ToString() == valor)

if(!existe)
{
    //aqui insertas
}

with this applies a search on the data in memory that are in the datatable loaded

    
answered by 15.12.2016 в 22:36