2016-10-10 2 views
3

중복 레코드를 차단할 수 있습니까? 또한 선택한 레코드를 편집 할 수 있습니까? 나는 이것과 비슷한 것을 연구 중이다. ExampleDataGridView 레코드 편집을 차단했습니다.

많은 레코드가 있고 2 번째 또는 3 번째 레코드를 업데이트 할 때 첫 번째 레코드의 ID를 사용할 때마다 경고 메시지가 나타납니다. 그것은 잘 작동합니다. 그러나 첫 번째 레코드의 이름, 도시 등을 편집하려고 할 때마다 ID를 변경하지 않았으므로 중복 ID 오류가 표시됩니다. 그것은 자신을 복제본으로 간주하고있다. 어떻게해야할지 모르겠다. 내가 중단 점을 사용하여 시도했지만 관심이 아무것도 볼 수 없습니다. 감사.

private void btnUpdate_Click(object sender, EventArgs e) 
    { 
     if (dgvProfiles.SelectedCells.Count <= 0) 
     { 
      MessageBox.Show("No record was selected to update."); 
     } 

     else { 
      for (int row = 0; row < dgvProfiles.Rows.Count; row++) 
      { 
       for (int col = 0; col < dgvProfiles.Columns.Count; col++) 
       { 
        if (dgvProfiles.Rows[row].Cells[col].Value != null && 
         dgvProfiles.Rows[row].Cells[col].Value.Equals(txtEmail.Text.Trim())) 
        { 
         MessageBox.Show("Duplicate email was entered."); 
         return; 
        } 

        else if (dgvProfiles.Rows[row].Cells[col].Value != null && 
         dgvProfiles.Rows[row].Cells[col].Value.Equals(txtID.Text.Trim())) 
        { 
         MessageBox.Show("Duplicate ID was entered."); 
         return; 

        } 
       } 
      } 
      DataGridViewRow newDataRow = dgvProfiles.Rows[indexRow]; 
      newDataRow.Cells[0].Value = txtID.Text; 
      newDataRow.Cells[1].Value = txtName.Text; 
      newDataRow.Cells[4].Value = txtEmail.Text; 
      newDataRow.Cells[5].Value = txtCity.Text; 
      newDataRow.Cells[6].Value = cbxState.Text; 

     } 
    } 
+1

실제 질문에 대한 답변이 아니지만 모든 행의 모든 ​​열을 검색 할 필요는 없습니다. ID가 열 0에 있으므로 해당 열의 셀을 확인하고 전자 메일도 확인하십시오. 현재와 ​​같이 _ 셀 _이 ID 값과 일치하면 중복으로 간주됩니다. – stuartd

+1

당신이 추가 할 때 당신은 ID 중복 (그리고 그것들이 어디서 왔는지 분명하지 않음)만을 확인해야하는 것처럼 보입니다. – Plutonix

답변

1

Validation events을 사용해보십시오.

DataGridView를 DataSet에 바인딩하면 DataSet의 값을 걸어 중복을 쉽게 찾을 수 있습니다. DataSource property을 참조하십시오.

+0

대단히 감사합니다! –

관련 문제