Condition does not contain in Datagridview

Asked

Viewed 58 times

-3

Good night.

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

Below, the image of the two Datagridview’s that I need to buy:

inserir a descrição da imagem aqui

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 that the user knows which document is missing.

All I could do at the time was the opposite, display the lines that match the two Datagridview’s.

Follows 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?

  • not related to the question but, what are you using for this interface of datagridview ? thank you

  • Sincerely nothing. I just altered your properties.

  • this is not the standard look of the control, so the question, is not using any external component for this ?

  • Yes, this is the standard Datagridview control, all I did was edit its properties. If you want, I can make it available for download.

  • was pretty cool, don’t worry the curiosity was to know if it was something external even. Thank you

  • Thank you. It’s really important for me to get every possible feedback on my project.

  • About my problem, would you have any solution? Honestly, I don’t know what I do.

  • Make the logic in the classes, use datagridview only to display. Put the class you use to the notes I try to help you... I’m running out of time, but I’ll try

  • The problem is that these Grids receive data directly from an excel spreadsheet. Only the second Grid uses a class to better organize the data. So I try to do the analysis directly in Datagridview’s. I thought about doing this using Lists, because there is a Method called Except. But I did not study in depth how it works.

  • The first step is to create the classes that represent the lines, then work with the lists. Shows the code that you populate the grids

  • Follow the link: https://drive.google.com/file/d/1KT1br1pIgwWvdGTar_vHfDV89ssrO9XE/view?usp=sharing

Show 6 more comments

3 answers

0

You first have to think about the problem without displaying the data, after solved use Datagridview only for display.

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

First you need to define an object of your Notafiscal:

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 other 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;
}

Then 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 in any condition 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();

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

With the key list, 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 display them:

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

I made an example at .Netfiddle

I hope it will help solve the problem. I recommend that you access and read the Tour, because it is important to know how to participate in the community and to elaborate the questions. You can still improve your question by making it more specific and clear, thus gaining reputation and usually the negative votes are withdrawn.

  • 1

    I will test now in the evening and as soon as possible I leave my feedback. But I already want to thank you for the attention given to my question.

0

So without testing, only with Notepad++ really, 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 doesn’t work (since I haven’t tested), you can always change the way the list notExists is constructed 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 });
  • Hello, thank you so much for your attention to my question. I have analyzed your code and there are some syntax problems. I made a brief correction and adapted it.

0

Stayed 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:

inserir a descrição da imagem aqui

So I reversed the Datagridviews in the code to look like this:

if (radioButtonNotasAusentesConsisaNet.Checked == true)
            {
                foreach (DataGridViewRow linha in dataGridViewConsisaNet.Rows)
                {
                    int value = Convert.ToInt32(linha.Cells[2].Value);
                    var notExists = dataGridViewSefaz.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 });
                    }
                }
            }

But, what happens is that it repeats the values in the third Grid. So:

inserir a descrição da imagem aqui

Honestly, I don’t know what I can do to fix it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.