我有一个自定义对象与几个属性,其中之一返回一个列表。这是该对象的代码:如何将DataGridViewComboBoxColumn绑定到返回列表的对象的属性/方法?
public class SearchResult
{
private int eventId;
private String eventTitle;
private int startDate;
private List<String> tags;
// Properties
public int EventId { get { return this.eventId; } }
public String EventTitle { get { return this.eventTitle; } }
public int StartDate { get { return this.startDate; } }
public List<String> Tags { get { return this.tags; } }
public SearchResult(int eventId, String eventTitle, int startDate, List<String> tags)
{
// Constructor code
}
public List<String> GetTags()
{
return this.tags;
}
}
我也有一个DataGridViewComboBoxColumn
,我想绑定到Tags
财产。基本上,每个SearchResult
对象将显示在其自己的行中,并且我希望在每个对象的Tags
属性中的List<String>
显示在该行的ComboBox
单元格中。这是我至今对我DataGridView
代码:
BindingList<SearchResult> results = new BindingList<SearchResult>();
results.Add(new SearchResult(1, "This is a title", 2012, new List<String> { "Tag1", "Tag with a long name1" }));
results.Add(new SearchResult(2, "The quick brown fox", 2012, new List<String> { "Stack", "Overflow" }));
results.Add(new SearchResult(3, "In this tutorial, you create a class that is the type for each object in the object collection. ", 2012, new List<String> { "NYSE", "FTSE" }));
results.Add(new SearchResult(4, "another long piece of title text", -999, new List<String> { "Rabbits", "Chickens" }));
MyDataGrid.AutoGenerateColumns = false;
MyDataGrid.AllowUserToAddRows = false;
MyDataGrid.AllowUserToDeleteRows = false;
MyDataGrid.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.None;
MyDataGrid.BackgroundColor = System.Drawing.SystemColors.Control;
MyDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
MyDataGrid.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
MyDataGrid.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCells;
MyDataGrid.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
DataGridViewTextBoxColumn eventIdColumn = new DataGridViewTextBoxColumn();
eventIdColumn.DataPropertyName = "EventId";
eventIdColumn.HeaderText = "Event ID";
eventIdColumn.ReadOnly = true;
eventIdColumn.Width = 84;
DataGridViewTextBoxColumn eventTitleColumn = new DataGridViewTextBoxColumn();
eventTitleColumn.DataPropertyName = "EventTitle";
eventTitleColumn.HeaderText = "Event Title";
eventTitleColumn.ReadOnly = true;
eventTitleColumn.Width = 368;
DataGridViewTextBoxColumn startDateColumn = new DataGridViewTextBoxColumn();
startDateColumn.DataPropertyName = "StartDate";
startDateColumn.HeaderText = "Start Date";
startDateColumn.ReadOnly = true;
startDateColumn.Width = 130;
//I think I need to insert the code for the tags column here, but I'm not sure
MyDataGrid.Columns.Add(eventIdColumn);
MyDataGrid.Columns.Add(eventTitleColumn);
MyDataGrid.Columns.Add(startDateColumn);
//MyDataGrid.Columns.Add(tagsColumn);
MyDataGrid.DataSource = results;
我得出这个代码a tutorial I found online,和它完美的作品。
我一直在试图将SearchResult
的Tags
属性绑定到DataGridViewComboBoxColumn
,但我不确定如何。我一直在寻找this question,它提供了这样的代码:
column.DataPropertyName = "Foo";
column.DisplayMember = "SomeNameField";
column.ValueMember = "Bar"; // must do this, empty string causes it to be
// of type string, basically the display value
// probably a bug in .NET
column.DataSource = from foo in Foo select foo;
grid.DataSource = data;
我在遇到麻烦的原因是因为链接的问题,我不明白的一些细微差别的。
- 根据the documentation,以及相关的问题,
DisplayMember
应该链接到“包含实例的说明”的属性,但由于SearchResult
对象动态添加或不具有与它们相关的任何说明,应我只是把它留空? ValueMember
给了我类似的问题,因为即使在阅读its documentation后,我仍然不确定要放什么。- 在链接问题中,接受的答案使用LINQ立即绑定整个数据网格。那我应该怎么做?我不确定如何为我的情况修改该代码,但我认为这将是沿着这些路线的东西。
:
tagsColumn.DataPropertyName = "Tags";
tagsColumn.DisplayMember = ""; // I'm unsure of what to put here
tagsColumn.ValueMember = ""; // Once again, I don't know what to set this to
我还假设我应该具有设置DataSource
该列的线,例如
tagsColumn.DataSource = <some LINQ query, perhaps?>
但我不知道,因为唯一最相关的C#源代码,我已经能够找到这个问题。
UPDATE:
我发现a second question这表明数据绑定类似下面的代码:此基础上
// reference the combobox column
DataGridViewComboBoxColumn cboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns[0];
cboBoxColumn.DataSource = Choice.GetChoices();
cboBoxColumn.DisplayMember = "Name"; // the Name property in Choice class
cboBoxColumn.ValueMember = "Value"; // ditto for the Value property
,我一个)的添加GetTags()
方法SearchResult
,并将此代码到我的DataGridView
初始化代码:
DataGridViewComboBoxColumn tagsColumn = new DataGridViewComboBoxColumn();
tagsColumn.DataSource = SearchResult.GetTags(); // ERROR
tagsColumn.DisplayMember = ""; // Still not sure
tagsColumn.ValueMember = ""; // ??
但是,Visual Studio的给我上的S错误当我尝试运行此的Econd行:
An object reference is required for the non-static field, method, or property 'SearchResult.GetTags()'
更新2:
我还在摸索这个没有成功。我不明白如何与其他属性(例如EventId
)我可以简单地声明数据属性名称为EventId
,它将显示在表中,但我不能这样做与ComboBox
列。
由于对象是在一个单独的类中实例化并放入一个列表中,所以我不应该通过整个对象数组(其中可能有几百个)进行循环,将Tags
属性绑定到每个实例的ComboBox
列,当我不需要遍历SearchResult
对象的列表来绑定其他属性,例如EventId
。
为什么此绑定属性的名称只适用于某些属性而不是其他属性?
错误来自您调用SearchResult.GetTags();您需要将其设为静态方法,或者首先实例化SearchResult(新...) – blearn
@blearn在第二个链接问题的答案中,该对象未实例化。另外,由于'SearchResult'对象会动态地添加到这个列表中,所以在一个单独的类中的方法中,除非我遍历它们,否则我不一定能够访问它们,在这种情况下,我可能只是跳过数据绑定,手动填充'DataGridView',我试图避免这么做。 –