![](/img/trans.png)
[英]Select Item in DataGridViewComboBoxColumn loaded from SQL
[英]DataGridViewComboBoxColumn does not allow me to select an object from a list
我有一個DataGridView,它允許用戶將新對象添加到列表中。 其中的重要部分之一是從用戶可定義的列表中選擇類型。
我正在定義列,如下所示:
this.DataGridView.Columns.Add(new DataGridViewComboBoxColumn
{
Name = "Resource",
DataPropertyName = "Resource",
DataSource = new BindingSource { DataSource = this.Document.Resources },
ValueType = typeof(Resource),
DisplayMember = "Name"
});
然后,將DataGridView的DataSource設置為UserResources的列表:
BindingList<UserResource> relatedResources = new BindingList<UserResource>(this.User.ResourcesRequired);
this.DataGridView.DataSource = relatedResources;
Resource類的布局如下所示:
public class Resource
{
public string Name { get; set; }
public string Description { get; set; }
public int InitialLevel { get; set; }
}
UserResource類如下所示:
public class UserResource
{
public Resource Resource { get; set; }
public int CurrentLevel { get; set;
}
User類如下所示:
public class User
{
public string Name { get; set; }
public IEnumerable<UserResource> Resources { get; set; }
}
我可以看到可用資源類型的列表,但是在DataGridViewComboBoxCell中選擇的項目不會保持選中狀態。 選擇項目后,當我繼續下一個字段時,DataGridViewComboBoxCell會清除自身。
該行中的其他字段將被寫入新的UserResource實例,但不會保存Resource引用,並且該屬性在新的UserResource實例上保持為null。
如果人們想知道,我根本就沒有使用任何類型的對象關系映射器或任何類型的數據庫層。 內存中的所有對象都將寫入和讀取XML文檔。
老實說,我不確定如何從此處繼續調試此問題。 有人有建議嗎?
好的,我做了一些更改。 現在,起初它可以正常工作。 將資源類別更改為:
public class Resource
{
public string Name { get; set; }
public string Description { get; set; }
public int InitialLevel { get; set; }
//added by dx
public new string ToString()
{
return Name;
}
}
為網格組合框創建新的類:
public class ComboSource
{
public string Name
{
get
{
if (SourceValue != null)
return SourceValue.ToString();
return string.Empty;
}
}
public Resource SourceValue{ get; set; }
}
用法示例:
private List<ComboSource> resources = new List<ComboSource>();
this.resources.Add(new ComboSource() { SourceValue = new Resource() { Name = "rs1", Description = "a"} });
this.resources.Add(new ComboSource() { SourceValue = new Resource() { Name = "rs2", Description = "b" } });
this.resources.Add(new ComboSource() { SourceValue = new Resource() { Name = "rs3", Description = "c" } });
和
this.dataGridView1.Columns.Add(new DataGridViewComboBoxColumn
{
Name = "Resource",
DataPropertyName = "Resource",
ValueMember = "SourceValue",
DataSource = new BindingSource { DataSource = this.resources },
ValueType = typeof(Resource),
DisplayMember = "Name"
});
我認為問題不在於此控件。 您可以選擇合適的項目。 正如您所說,當您移動表格時,該項目將被清除。
這意味着還有其他來源正在對此控件進行驗證並決定清除它。 請查找代碼的其他部分或鈎到change事件的鈎子,這些鈎子可能正在修改所選的值。
我願意提供更多幫助,但我需要查看完整的代碼,如果您願意分享,則我的電子郵件地址在我的個人資料中。
<telerik:DataFormComboBoxField Grid.Row="0" Grid.Column="2" Label="Time Zone:" LabelPosition="Above">
<telerik:RadComboBox SelectedValue="{Binding TimeZoneID, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
ItemTemplate="{StaticResource DataTemplateTimeZone}"
SelectedValuePath="ID"
ItemsSource="{Binding DataSource.TimeZoneDataSource, Source={StaticResource vmProxy}}"
IsEnabled="True"/>
</telerik:DataFormComboBoxField>
我建議使用包含可觀察性集合的視圖模型,並使用http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding- in-nested-controls.aspx
就像我對StaticResource vmProxy所做的一樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.