2012-09-20 3 views
0

사용자가 레코드를 직접 추가 할 수있게하려는 DataGridView가 있습니다. 이 작업은 DGV 아래의 링크를 클릭하여 수행됩니다.이 때 새 행이 프로그래밍 방식으로 추가되고 첫 번째 보이는 셀은 ComboBoxCell 유형입니다.DGV ComboBox 셀을 계속 업데이트 할 수 있습니다.

// Add a new row to DGV 
DataGridView dgv = this.dgvInformation; 
DataGridViewRow newRow = new DataGridViewRow(); 
dgv.Rows.Add(newRow); 

// Create cells and add to row 
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell(); 
newRow.Cells["InfoType"] = cellInfoType; 

// Create DataSource based off LINQ query here 
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>(); 
// List is populated here 

// Assign DataSource to combo cell 
cellInfoType.DataSource = comboDataSource; 
cellInfoType.ValueMember = "AccInfoTypeID"; 
cellInfoType.DisplayMember = "InfoType"; 

// Scroll new row into view and begin editing 
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1; 
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1]; 
dgv.BeginEdit(true); 

데이터 소스는 사용자가 선택할 수 없습니다해야 범주는 -1의 ID와 일부 값을 포함하고, 다른 모든 값은 유효한이 : 어떻게이 일을 해요위한 코드 발췌입니다 데이터베이스 ID. 이 모든 것은 잘 작동합니다. 단, 사용자가 -1 행을 선택하면 콤보 셀을 더 많이 편집 할 수 없으며 셀에 값을 저장하기 만하면 더 이상 드롭 다운을 활성화 할 수 없습니다. 나는 _CellValueChanged 이벤트에 다음 코드를 추가했지만 아무 효과가 없습니다 :

private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
{ 
    DataGridView dgv = this.dgvInformation; 
    DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex]; 

    // If type of cell is ComboBox then 
    if (cell.GetType() == typeof(DataGridViewComboBoxCell)) 
    { 
     DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell; 

     if ((int)cellInfoType.Value == -1) 
     { 
      MessageBox.Show("Going back into edit mode..."); 
      dgv.CurrentCell = cell; 
      dgv.BeginEdit(true); 
     } 
     else 
     { 
      MessageBox.Show(cellInfoType.Value.ToString()); 
     } 
    } 
} 

내가 다른 셀에 이동 한 후 여기에 "... 다시 편집 모드로 전환"메시지가 할을하지만, '아무튼 그러면 그 말을해라. 누구나 편집 모드로 돌아 가지 않을 이유를 설명해 주시겠습니까? 아니면 값을 선택하자 마자 값이 잠기지 않게하는 방법이 있습니까?

감사합니다.

+0

질문을 쉽게 이해할 수 없습니다. Datagridview는 기본적으로 편집 가능합니다. 왜'dgv.BeginEdit (true); '를 사용해야할까요? – Sami

+0

DGV EditMode가 EditProgrammatically로 설정되어 있습니다. 사용자가 더블 클릭으로 업데이트하거나 항목 추가 링크를 클릭하기를 원하기 때문입니다. 또한 편집 가능한 열은 TextBox로 표시되며 편집 할 때만 콤보입니다. – CrazyHorse

답변

1

값이 -1 인 경우 가로 채고 취소 할 수있는 "유효성 검사"이벤트가 있어야합니다.

+0

고마워요. 속임수가되었습니다. – CrazyHorse

+0

표시되는 텍스트 인 FormattedValue를 쿼리 할 수있는 것 같습니다. 가능한 실제 값을 쿼리 할 수 ​​있습니까? 텍스트로 작업 할 수 있지만 – CrazyHorse

+0

ID를 쿼리하는 것이 더 편리 할 것입니다. 이제 답변을 찾았습니다. DGV의 EditingControl을 ComboBox (DataGridViewComboBoxCell이 아닌)로 캐스팅해야합니다. 그러면 참조 할 수 있습니다. SelectedValue 속성 – CrazyHorse