Home > OS >  How to compare cells of one column for selected rows?
How to compare cells of one column for selected rows?

Time:02-26

Hi everyone please i need you're help if it's possible: i have a DGV with 8 columns i want to compare all cells of second column "Fournisseur" whene rows are selected.

  • image1: selected rows have the same value of second column "Tramauto"="Tramauto"
  • image2: selected rows have differents values of sec column 1row "Tramauto"= 3row "Tramauto" but 2row different "Sonasid"

If=True

If=False

private void button13_Click(object sender, EventArgs e)
    {
      foreach (DataGridViewRow row in commandeDataGridView.SelectedRows)
            {
                 if(//selected rows have the same value of second column)
                    {do function}

               else
            {

                string message = "shoose the same Fournisseur";
                string title = "Error";
                MessageBox.Show(message,title);
            }

Thank you so much for you're help!

CodePudding user response:

You could use:

List<string> allCol2Values = commandeDataGridView.SelectedRows.Cast<DataGridViewRow>()
    .Select(r => r.Cells[1].Value?.ToString())
    .ToList();
string first = allCol2Values.FirstOrDefault();
bool allSame = allCol2Values.Skip(1).All(s => s == first);
  •  Tags:  
  • c#
  • Related