문제

DataGridView 내에서 별도의 ComboBox 셀을 사용자 정의 클래스에 바인딩하려고합니다.

DataGridViewComboBoxCell 값은 유효하지 않습니다

나는 현재 셀의 데이터 소스를 IList<ICustomInterface> 내가 가진 사전에서. 그러나 데이터 소스를 설정하면 ComboBoxCell 설정되지 않으므로 유효하지 않은 값이 선택되었습니다.

나는 실제 값을 선택하는 방법, 예를 들어이 오류를 제거하기 위해 주어진 목록 내 0 번째 항목을 선택하거나 문제를 해결하는 다른 방법을 찾으려고 노력하고 있습니다. 누구든지 제안이 있습니까?

도움이 되었습니까?

해결책

질문을 게시 한 후 얼마 지나지 않아 솔루션을 찾을 수있었습니다. 다른 사람을 위해 :

문제는 내가 할당하려고했다는 것입니다. DataGridViewComboBoxCell.Value 셀에 셀이 데이터 소스에 바인딩 되었기 때문에 소스 및 업데이트에서 자동으로 객체를 찾을 것으로 기대합니다.

이것은 실제로는 그렇지 않았으며, 실제로 값을 동일하게 설정해야합니다. ValueMember 값과 바인딩을 올바르게 업데이트 할 수있는 속성. 나는 둘 다에 대한 속성 '이름'을 사용하고 있다고 생각합니다. ValueMember 그리고 DisplayMember (셀의 렌더링 방법을 제어) 값을 설정하십시오. interface.ToString() (인터페이스 인스턴스 대신)는 대부분의 경우에 작동합니다. 그런 다음 주변의 소스를 변경하는 동안 발생하는 DataError 예외를 잡고 무시합니다.

다른 팁

열거를 사용할 때 간단한 솔루션이 있습니다

ColumnType.ValueType = typeof (MyEnum);
ColumnType.DataSource = Enum.GetValues(typeof (MyEnum));

"InitializeComponent ();"직후에 그렇게 할 수 있습니다.

시련의 시간이 지난 후 마침내 작동하는 해결책을 찾았습니다.

// Create a DataGridView
System.Windows.Forms.DataGridView dgvCombo = new System.Windows.Forms.DataGridView();

// Create a DataGridViewComboBoxColumn
System.Windows.Forms.DataGridViewComboBoxColumn colCombo = new 

System.Windows.Forms.DataGridViewComboBoxColumn();

// Add the DataGridViewComboBoxColumn to the DataGridView
dgvCombo.Columns.Add(colCombo);

// Define a data source somewhere, for instance:
public enum DataEnum
{
    One,
    Two,
    Three
}

// Bind the DataGridViewComboBoxColumn to the data source, for instance:
colCombo.DataSource = Enum.GetNames(typeof(DataEnum));

// Create a DataGridViewRow:
DataGridViewRow row = new DataGridViewRow();

// Create a DataGridViewComboBoxCell:
DataGridViewComboBoxCell cellCombo = new DataGridViewComboBoxCell();

// Bind the DataGridViewComboBoxCell to the same data source as the DataGridViewComboBoxColumn:
cellCombo.DataSource = Enum.GetNames(typeof(DataEnum));

// Set the Value of the DataGridViewComboBoxCell to one of the values in the data source, for instance:
cellCombo.Value = "Two";
// (No need to set values for DisplayMember or ValueMember.)

// Add the DataGridViewComboBoxCell to the DataGridViewRow:
row.Cells.Add(cellCombo);

// Add the DataGridViewRow to the DataGridView:
dgvCombo.Rows.Add(row);

// To avoid all the annoying error messages, handle the DataError event of the DataGridView:
dgvCombo.DataError += new DataGridViewDataErrorEventHandler(dgvCombo_DataError);

void dgvCombo_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    // (No need to write anything in here)
}

그게 다야.

나는 같은 문제가 있었다.

내 경우 솔루션은 외래 키 테이블의 데이터 어댑터를 채우는 것이 었습니다. 이것은 자동으로 채워지지 않았으며 이것이 문제의 원인이었습니다.

에서 Page_Load 이벤트:

Me.TblUserTypesTableAdapter.Fill(Me.DonateDataSet.tblUserTypes)

나는 같은 문제가 있습니다. (Unbouod) Datagrid에서 Combobox 열을 채운 후, 나는 Valuember 속성을 설정하여 문제를 해결했습니다. DataGridViewComboBoxColumn분명히, 그냥 의지합니다 ToString() Combobox의 물체의 속성으로는 충분하지 않습니다.

실제 코드 :

/// <summary>
/// Populate the dataGridSplitVolumes data grid with all existing qualifications for the plan.
/// </summary>
/// <param name="bonus"></param>
private void PopulateDataGridSplitVolumes(Bonus_Group bonus)
{
  try
  {
    List<Qualification> qualifications = Qualification.Load(this.groupBonus.PlanID, this.ConnectionString);
    foreach (Qualification qual in qualifications)
    {
      DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)this.dataGridSplitVolumes.Columns[0];
      col.Items.Add(qual);                    
    }
    SplitVolumeGrid_QualificationColumn.ValueMember = "Name";
  }
  catch (Exception ex)
  {
#if DEBUG
    System.Diagnostics.Debugger.Break();
#endif
    throw ex;
  }
}//PopulateDataGridSplitVolumes     

DataError 이벤트 처리기 사용

private void shahriartableDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //You don't have to write anything here !
        }

그리고 당신의 displaymember 및 valuember 데이터 유형은 동일해야합니다. 제 경우에는 두 가지 모두에 대한 국가 이름이 있습니다. 모든 것이 나에게 잘 작동합니다 ... !!

셀에 널 값을 설정하십시오.

dataGridView.CurrentRow.Cells[NAME].Value = null;

사람들을 위해 내가했던 것만 큼 어려움을 겪지 않는 사람들을 위해.

콤보를 바인딩 할 때 DisplayMember (사용자가 볼 수있는 것)를 설정하고 valuemember (응용 프로그램을 얻는 것)를 설정합니다.

이것들을 설정 한 후에는 값을 설정해야하며 이것이 실패한 곳입니다. 기본적으로 값의 유형은 Valuemember와 동일한 유형이어야합니다.

따라서 귀하의 값 멤버가 ID 유형 인 ID 인 경우 셀. value = 1;과 같은 int로 값을 설정해야합니다.

나는 같은 문제를 겪고 있었다. 메시지는 100% 자리를 차지했습니다. Combobox의 값은 다음과 같습니다. Exact, Startswith ... 그리고 나는 값을 정확하게 설정하려고했습니다 (정확하지 않음). DataTable.ReadXML (...)을 사용하여 .xml 파일에서 DataGridView의 Datatable을 읽을 때 자동으로 발생했습니다. .xml 파일의 값이 꺼져 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top