에서 서로 다른 제어 유형이 양식?윈도우 DataGridView 컨트롤이는 DataGridView를에서 다음을 수행 할 수있는 동일한 열
(이는 때때로 내가 드롭 다운 목록을 표시하기 때문에 사용자가 자유 값을 입력하기를 원하기 때문입니다.) 감사합니다,
P. C#을 사용 중입니다
에서 서로 다른 제어 유형이 양식?윈도우 DataGridView 컨트롤이는 DataGridView를에서 다음을 수행 할 수있는 동일한 열
(이는 때때로 내가 드롭 다운 목록을 표시하기 때문에 사용자가 자유 값을 입력하기를 원하기 때문입니다.) 감사합니다,
P. C#을 사용 중입니다
DataGridViewCell에서 상속받은 자신 만의 클래스를 만들고 적절한 가상 멤버를 재정의 할 수 있습니다 (InitializeEditingControls, EditType, 기타 몇 가지). 그런 다음
감사합니다. 올바른 궤적처럼 보입니다. 불행하게도 나는 이것에 대해 새삼스러워서 내가하는 일을 하던지 세포를 나타나게 할 수는 없다. 제안 된대로 EditType을 재정의했습니다. 만약 내가 'DataGridViewTextBoxCell'에서 상속받은 다음 'DataGridViewCell'을 상속 받고 typeof (DataGridViewTextBoxEditingControl)를 반환하기 위해 EditType을 재정 의하여 빈 셀을 제공하는 셀을 볼 수 있습니다. 그래도 열을 볼 수 있습니다. 나는 분명히 내가 놓친 뭔가가 있다고 확신한다. –
뷰 모드 컨트롤이 보이지 않거나 0 인 경우 너비가있는 것 같습니다. 컨트롤에 탭하여 F2 키를 치고 값을 입력 할 수 있습니다. 종료하면 편집 할 수있는 모드로 돌아갈 때 볼 수 없습니다. –
PositionEditingPanel과 PositionEditingControl도 재정의해야한다고 생각합니다. 하지만 직접 해본 적이 없으므로 추측 할 수는 있습니다 ... 표준 DataGridViewCell에 대한 코드를 보려면 Reflector를 사용해야합니다. 아마도 당신이해야 할 것을 보여줄 것입니다 –
두 컨트롤을 모두 포함하는 템플릿 열을 만들거나 원하지 않는 것을 숨기고 다른 하나를 코드의 데이터 소스에 바인딩 할 수 있습니다.
템플릿 열은 ASP.NET GridView 용이며 WinForms DataGridView 용은 아닙니다 ... –
Ahh, my bad. WinForms로 작업 한 이후로 꽤 오래되었습니다. – atfergs
내가 최근에 비슷한 쓰임새를 했어 셀 템플릿으로이 클래스의 인스턴스와의 DataGridViewColumn을 만들고 아래 코드 같은 것을 작성 결국 수 있습니다
하는 사용자 정의 셀 및 열 클래스를 작성을 그리고, 셀의 EditType 및 InitializeEditingControl 메소드를 오버라이드 (난 그냥 사용을 제어 무엇을 나타내는 .useCombo 필드 사용자 정의 클래스의 목록에 데이터 바인딩하고있어 여기에) 적절한 다른 컨트롤을 반환 :
// Define a column that will create an appropriate type of edit control as needed.
public class OptionalDropdownColumn : DataGridViewColumn
{
public OptionalDropdownColumn()
: base(new PropertyCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a PropertyCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(PropertyCell)))
{
throw new InvalidCastException("Must be a PropertyCell");
}
base.CellTemplate = value;
}
}
}
// And the corresponding Cell type
public class OptionalDropdownCell : DataGridViewTextBoxCell
{
public OptionalDropdownCell()
: base()
{
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
DataItem dataItem = (DataItem) this.OwningRow.DataBoundItem;
if (dataItem.useCombo)
{
DataGridViewComboBoxEditingControl ctl = (DataGridViewComboBoxEditingControl)DataGridView.EditingControl;
ctl.DataSource = dataItem.allowedItems;
ctl.DropDownStyle = ComboBoxStyle.DropDownList;
}
else
{
DataGridViewTextBoxEditingControl ctl = (DataGridViewTextBoxEditingControl)DataGridView.EditingControl;
ctl.Text = this.Value.ToString();
}
}
public override Type EditType
{
get
{
DataItem dataItem = (DataItem)this.OwningRow.DataBoundItem;
if (dataItem.useCombo)
{
return typeof(DataGridViewComboBoxEditingControl);
}
else
{
return typeof(DataGridViewTextBoxEditingControl);
}
}
}
}
그런 다음이 유형의 DataGridView에 열을 추가하면 올바른 편집 컨트롤이 있어야합니다. sed.
+1, 이것은 매우 유용하고 깨끗합니다. 이것에 대한 멋진 점은 EditType getter에서 "just in time"에 필요한 편집 컨트롤 클래스에 신호를 보내는 것만으로 Grid가 힘들이지 않고 DataGridView.EditingControl을 참조하여 생성 된 편집 컨트롤을 사용할 수 있습니다. – TheBlastOne
아주 좋은 질문입니다. DataGridView 클래스를 사용할 때이 문제가 발생할 때까지는 시간 문제 일뿐입니다. – TheBlastOne