Condition does not contain in DataGridView

-3

Good evening.

I need to make a comparison between two DataGridView's and display in a third DataGridView, the first lines that is not contained in the second.

Below is the image of the two DataGridView's that I need to buy:

Note that, in the first DataGridView, there is a line with document 63989, and in the second DataGridView, this record is missing.

My goal then is to display this line that is missing in a third DataGridView so the user will know which document is missing.

All I was able to do at the time was the opposite, displaying the lines that match the two DataGridView's.

Follow the code:

private void btAnalisar_Click(object sender, EventArgs e)
{
     if (radioButtonNotasAusentesConsisaNet.Checked == true)
     {
         for (int i = 0; i < dataGridViewSefaz.Rows.Count; i++)
         {
             for (int index = 0; index < dataGridViewConsisaNet.Rows.Count; index++)
            {
                if (dataGridViewSefaz.Rows[i].Cells[2].Value.ToString().Equals(dataGridViewConsisaNet.Rows[index].Cells[2].Value.ToString()))
                {
                    dataGridViewAnalise.Rows.Insert(0, dataGridViewSefaz.Rows[i].Cells[2].Value.ToString(), dataGridViewSefaz.Rows[i].Cells[3].Value.ToString(), dataGridViewSefaz.Rows[i].Cells[24].Value.ToString());
                }
            }
        }
    }
}

How can I proceed?

    
asked by anonymous 04.08.2018 / 03:12

3 answers

0

So without testing, just with Notepad ++, I think this would solve:

private void btAnalisar_Click(object sender, EventArgs e)
{
    if (radioButtonNotasAusentesConsisaNet.Checked)
    {
        foreach(var dataRow in dataGridViewSefaz.Rows)
        {
            int value = Convert.ToInt32(dataRow.Cells[2].Value);
            var notExists = dataGridViewConsisaNet.Rows.Cast<DataGridViewRow>().
                Where(r => Convert.ToInt32(r.Cells[2].Value) != value).
                Select(x => new DataGridViewRow() { x.Cells[2].Value, x.Cells[3].Value, x.Cells[24].Value });

            if(notExists.Length > 0)
                dataGridViewAnalise.Rows.AddRange(notExists);
        }
    }
}

If it does not work (since I have not tested it), you can always change how the notExists list is built and is inserted in the 3rd grid:

var notExists = dataGridViewConsisaNet.Rows.Cast<DataGridViewRow>().
    Where(r => Convert.ToInt32(r.Cells[2].Value) != value);

foreach(var dataRow in notExists)
    dataGridViewAnalise.Rows.Add(new object[] { x.Cells[2].Value, x.Cells[3].Value, x.Cells[24].Value });
    
04.08.2018 / 13:40
0

It looks like this:

if (radioButtonNotasAusentesConsisaNet.Checked == true)
{
 foreach (DataGridViewRow linha in dataGridViewSefaz.Rows)
 {
  int value = Convert.ToInt32(linha.Cells[2].Value);
  var notExists = dataGridViewConsisaNet.Rows.Cast<DataGridViewRow>().Where(r => Convert.ToInt32(r.Cells[2].Value) != value);
  foreach (var dataRow in notExists)
  {
   dataGridViewAnalise.Rows.Add(new object[] { dataRow.Cells[2].Value, dataRow.Cells[3].Value, dataRow.Cells[24].Value });
  }
 }
}

However, when running, this exception is displayed:

SoIinvestedtheDataGridViewsinthecodetolooklikethis:

if(radioButtonNotasAusentesConsisaNet.Checked==true){foreach(DataGridViewRowlinhaindataGridViewConsisaNet.Rows){intvalue=Convert.ToInt32(linha.Cells[2].Value);varnotExists=dataGridViewSefaz.Rows.Cast<DataGridViewRow>().Where(r=>Convert.ToInt32(r.Cells[2].Value)!=value);foreach(vardataRowinnotExists){dataGridViewAnalise.Rows.Add(newobject[]{dataRow.Cells[2].Value,dataRow.Cells[3].Value,dataRow.Cells[24].Value});}}}

ButwhathappensisthatitrepeatsthevaluesinthethirdGrid.So:

I honestly do not know what I can do to fix this.

    
04.08.2018 / 21:21
0

You first have to think about the problem without displaying the data, once resolved use the DataGridView just for display.

I made a small example, I hope it helps you understand the problem:

First you need to define an object from your Fiscal Note:

public class NotaFiscal
{
    public string Chave {get;set;}
    public string Emitente {get;set;}
    public decimal Total {get;set;}
}

Then you will get the data in this format, no matter if it comes from the database, excel, or another source:

public static List<NotaFiscal> GetNotasExcel()
{
    List<NotaFiscal> notas = new List<NotaFiscal>();

    notas.Add(new NotaFiscal(){ Chave = "123456ABC", Emitente = "Empresa A", Total = 10 });
    notas.Add(new NotaFiscal(){ Chave = "223456DEF", Emitente = "Empresa B", Total = 20 });
    notas.Add(new NotaFiscal(){ Chave = "423456JKL", Emitente = "Empresa D", Total = 40 });
    notas.Add(new NotaFiscal(){ Chave = "623456PQR", Emitente = "Empresa F", Total = 60 });
    notas.Add(new NotaFiscal(){ Chave = "723456STU", Emitente = "Empresa G", Total = 70 });
    notas.Add(new NotaFiscal(){ Chave = "823456VWX", Emitente = "Empresa H", Total = 80 });


    return notas;
}

public static List<NotaFiscal> GetNotasConsisaNet()
{
    List<NotaFiscal> notas = new List<NotaFiscal>();

    notas.Add(new NotaFiscal(){ Chave = "123456ABC", Emitente = "Empresa A", Total = 10 });
    notas.Add(new NotaFiscal(){ Chave = "223456DEF", Emitente = "Empresa B", Total = 20 });
    notas.Add(new NotaFiscal(){ Chave = "323456GHI", Emitente = "Empresa C", Total = 30 });
    notas.Add(new NotaFiscal(){ Chave = "423456JKL", Emitente = "Empresa D", Total = 40 });
    notas.Add(new NotaFiscal(){ Chave = "523456MNO", Emitente = "Empresa E", Total = 50 });
    notas.Add(new NotaFiscal(){ Chave = "623456PQR", Emitente = "Empresa F", Total = 60 });
    notas.Add(new NotaFiscal(){ Chave = "723456STU", Emitente = "Empresa G", Total = 70 });
    notas.Add(new NotaFiscal(){ Chave = "823456VWX", Emitente = "Empresa H", Total = 80 });


    return notas;
}

So you store the data in lists, so you can work with them:

List<NotaFiscal> notasExcel =  GetNotasExcel();
List<NotaFiscal> notasConsisaNet =  GetNotasConsisaNet();

Now, you can find the data, under the conditions you want. Taking advantage of your case, I made an example to select all the notes that are in the "ConsisaNET" and are not in the Excel file:

List<string> chavesExcept = notasConsisaNet.Select(x => x.Chave).Except(notasExcel.Select(x=>x.Chave)).ToList();
  

Notice that I had to select all the keys (string) to compare with the keys of the other list. This is because if you compare two objects they will be compared by the instance, so even though the data is the same, they are different instances and would not be returned in that clause.

With the list of keys, you can now select the desired notes (if necessary):

List<NotaFiscal> notasExcept = notasConsisaNet.Where(x => chavesExcept.Contains(x.Chave)).ToList();

Ready! You already have all the data you need in memory. Just view them:

dataGridViewExcept.DataSource = notasExcept;
dataGridViewNotasExcel.DataSource = notasExcel;
dataGridViewNotasConsisa.DataSource = notasConsisaNet;

I've done an example in .NETFiddle

  

I hope it helps solve the problem. I recommend that you visit and read the Tour as it is important to know how to get involved in the community and to ask questions . You can still improve your question by making it more specific and clear, thus gaining a reputation and usually negative votes are removed.

    
06.08.2018 / 03:29