mardi 30 janvier 2018

Preventing duplicate values in several combo boxes with the same datasource in C#

I have five combo boxes all populated from a string dictionary that stores the header text from a datagridview. In my program, I want a message box to pop up either immediately a duplicate value is selected or when the OK button is pressed. The code below is an if statement that only works when other combo boxes duplicate the selectedvalue in the first combo box.

Is there a shorter way than this long if statement? and I want to be able to validate every other combo box so they won't have a duplicate. P.S I want the pop up to keep coming up till a unique value is selected. cbosort1,2,3..are the combo boxes names respectively.

if (cboSort1.SelectedValue == cboSort2.SelectedValue || cboSort1.SelectedValue == cboSort3.SelectedValue || cboSort1.SelectedValue == cboSort4.SelectedValue || cboSort1.SelectedValue == cboSort5.SelectedValue)
{
  MessageBox.Show("you cannot choose a duplicate column", "duplicate error");
return;
}

Aucun commentaire:

Enregistrer un commentaire