Я пытался заставить DataGridView работать с типами Nullable, на данный момент речь идет о пустом перечислении.
Извините за массивные блоки кода, которые я пытаюсь опубликовать как Как можно больше информации, я думаю, я немного усложнил это ... Я почти уверен, что это свойство комбинированного списка работает для обнуляемых типов при использовании вне DataGridView.
ComboBox cb = new ComboBox();
if (this.IsNullable)
{
cb.Items.Add("");
}
cb.Items.AddRange(this.PropertyType.GetEnumNames());
cb.DataBindings.Add(new Binding("Text", this, "Value", true, DataSourceUpdateMode.OnPropertyChanged, ""));
cb.Left = addLabel().Width + 2;
this.Width += cb.Left + cb.Width;
this.Controls.Add(cb);
Однако, когда я связываю Тип, который имеет свойство NULL .... Я не уверен, что делать дальше?
Создание DataGridView ...
// create a bindingsource here
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = this.PropertyType;
Binding b = new Binding("Tag", this, "Value", true, DataSourceUpdateMode.OnPropertyChanged, this.PropertyType);
b.BindingComplete += delegate (object sender, BindingCompleteEventArgs e)
{
if (this.value != null)
{
bindingSource.DataSource = this.value;
}
else
{
this.Value = Activator
.CreateInstance(typeof(List<>)
.MakeGenericType(this.PropertyType));
bindingSource.DataSource = this.value;
};
};
// just update the supportingFileList Placeholder if it is it.
if (this.PropertyType == typeof(Feature.supporting_file) && this.featureEditFrom != null)
{
DataGridView dgv = this.featureEditFrom.SupportingFilesList;
dgv.DataSource = bindingSource;
dgv.DataBindings.Add(b);
UseComboBoxForEnumsAndBools(dgv);
}
Класс, который я пытаюсь создать, например DataGridView. Список
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mynamesapce.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://mynamesapce.com", IsNullable=true)]
[System.Runtime.Serialization.DataContractAttribute(Name="supporting_file", Namespace="http://www.portbris.com.au/spatial.data")]
public partial class supporting_file : System.ComponentModel.INotifyPropertyChanged {
[EditorBrowsable(EditorBrowsableState.Never)]
private string fileNameField;
[EditorBrowsable(EditorBrowsableState.Never)]
private supporting_file_type fileTypeField;
[EditorBrowsable(EditorBrowsableState.Never)]
private System.Nullable<asset_status> fileStatusField;
[EditorBrowsable(EditorBrowsableState.Never)]
private System.Nullable<System.DateTime> fileDateField;
[EditorBrowsable(EditorBrowsableState.Never)]
private string fileCommentField;
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
[System.Runtime.Serialization.DataMemberAttribute()]
public string FileName {
get {
return this.fileNameField;
}
set {
if ((this.fileNameField != null)) {
if ((fileNameField.Equals(value) != true)) {
this.fileNameField = value;
this.OnPropertyChanged("FileName");
}
}
else {
this.fileNameField = value;
this.OnPropertyChanged("FileName");
}
}
}
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
[System.Runtime.Serialization.DataMemberAttribute()]
public supporting_file_type FileType {
get {
return this.fileTypeField;
}
set {
if ((fileTypeField.Equals(value) != true)) {
this.fileTypeField = value;
this.OnPropertyChanged("FileType");
}
}
}
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)]
[System.Runtime.Serialization.DataMemberAttribute()]
public System.Nullable<asset_status> FileStatus {
get {
return this.fileStatusField;
}
set {
if ((this.fileStatusField != null)) {
if ((fileStatusField.Equals(value) != true)) {
this.fileStatusField = value;
this.OnPropertyChanged("FileStatus");
}
}
else {
this.fileStatusField = value;
this.OnPropertyChanged("FileStatus");
}
}
}
[System.Xml.Serialization.XmlElementAttribute(DataType="date", IsNullable=true, Order=3)]
[System.Runtime.Serialization.DataMemberAttribute()]
public System.Nullable<System.DateTime> FileDate {
get {
return this.fileDateField;
}
set {
if ((this.fileDateField != null)) {
if ((fileDateField.Equals(value) != true)) {
this.fileDateField = value;
this.OnPropertyChanged("FileDate");
}
}
else {
this.fileDateField = value;
this.OnPropertyChanged("FileDate");
}
}
}
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=4)]
[System.Runtime.Serialization.DataMemberAttribute()]
public string FileComment {
get {
return this.fileCommentField;
}
set {
if ((this.fileCommentField != null)) {
if ((fileCommentField.Equals(value) != true)) {
this.fileCommentField = value;
this.OnPropertyChanged("FileComment");
}
}
else {
this.fileCommentField = value;
this.OnPropertyChanged("FileComment");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Test whether FileStatus should be serialized
/// </summary>
public virtual bool ShouldSerializeFileStatus() {
return FileStatus.HasValue;
}
/// <summary>
/// Test whether FileDate should be serialized
/// </summary>
public virtual bool ShouldSerializeFileDate() {
return FileDate.HasValue;
}
public virtual void OnPropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler handler = this.PropertyChanged;
if ((handler != null)) {
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("BentleyMap", "0.0.0.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://mynamesapce.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://mynamesapce.com", IsNullable=false)]
public enum supporting_file_type {
/// <remarks/>
Plan,
/// <remarks/>
Image,
/// <remarks/>
Document,
/// <remarks/>
Survey,
/// <remarks/>
Unspecified,
}