I want my combo box's value to be .Items[i]. Item is of type DataRowView.

My combo boxes are binded by database. Value member of cmbx is IDplayer in database. Selected value is playersBindingSource - IDplayer.

This is the error: Column 'IDplayer' is constrained to be unique. Value '2' is already present.

//cmb = Combo Box;
//cmbPlayers = Binding List of combo boxes

int i = 0;
foreach (ComboBox cmb in cmbPlayers)
{
    cmb.SelectedItem = cmb.Items[i]; // ERROR HAPPENS
    i++;
}

Im confused at what the issue is, as were the people on StackOverflow when you asked them the identical question.

Please clarify.

Then I'm really sorry. I don't know why I'm getting the exception which I named above. The exception is marked by the comment in code. Any ideas why it throw the exception?

The exception is thrown because the field IDplayer is a primary key field which is unique. You are trying to add the value '2' to a field withing that column and it already exists. Therefore the exception is thrown.

As to why it is thrown on that line, I can't say without understanding further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.