问题描述
我有几个属性,其中一个返回列表的定制对象。这是该对象的代码:
I have a custom object with several properties, one of which returns a list. This is the code for the object:
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
我要绑定到的标签属性。基本上,每个
属性在信息搜索结果
对象将显示在其自己的行,我想在列表<弦乐>
在标签组合框
细胞。这是代码我到目前为止对我的 DataGridView的
:
I also have a DataGridViewComboBoxColumn
that I want to bind to the Tags
property. Basically, each SearchResult
object will be displayed in its own row, and I want the List<String>
in the Tags
property of each object to be displayed in a ComboBox
cell in that row. This is the code I have so far for my 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;
我这个衍生代码的和它完美的作品。
我一直在试图信息搜索结果$ C的
标签
属性绑定$ C>到 DataGridViewComboBoxColumn
,但我不知道怎么样。我一直在看,该本提供代码:
I've been trying to bind the Tags
property of SearchResult
to a DataGridViewComboBoxColumn
, but I'm not sure how. I've been looking at this question, which provides this code:
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 reason I'm having trouble is because of a few nuances of the linked question that I don't understand.
- 据的和链接的问题,
的DisplayMember
应该链接到属性,载实例,但因为信息搜索结果
对象动态添加,并且没有任何与之关联的描述,我应该让它空白? $的说明b $ b -
ValueMember
是给我类似的问题,因为我不确定要放什么东西,甚至阅读的。 - 在链接的问题,接受的答案结合整个数据网格一次使用LINQ。那是我应该如何做呢?我不知道如何修改代码为我的情况,但我认为这将是东西沿着这些路线
- According to the documentation and the linked question,
DisplayMember
should be linked to the property that "contains a description of the instance", but sinceSearchResult
objects are added dynamically and don't have any description associated with them, should I just leave it blank? ValueMember
is giving me similar problems, since I'm unsure what to put even after reading its documentation.- In the linked question, the accepted answer binds the entire datagrid at once using LINQ. Is that how I should be doing this? I'm not sure how to modify that code for my situation, but I thought it would be something along these lines.
:
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
我也假设我应该有那台数据源
的列线,如:
tagsColumn.DataSource = <some LINQ query, perhaps?>
但我不知道,因为我已经能够找到的唯一的大多是有关C#源这个问题。
but I don't know because the only mostly relevant C# source I've been able to find is that question.
我没有找到的的第二个问题提出类似的代码,以这个数据绑定:
I did find a second question that suggests code similar to this for data binding:
// 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()
方法信息搜索结果
并添加以下代码到我的 DataGridView的
初始化代码:
Based on that, I a) added the GetTags()
method to SearchResult
and added this code into my DataGridView
initialisation code:
DataGridViewComboBoxColumn tagsColumn = new DataGridViewComboBoxColumn();
tagsColumn.DataSource = SearchResult.GetTags(); // ERROR
tagsColumn.DisplayMember = ""; // Still not sure
tagsColumn.ValueMember = ""; // ??
但是,Visual Studio的给我,当我尝试运行此第二行的错误:
However, Visual Studio gives me an error on the second line when I try to run this:
An object reference is required for the non-static field, method, or property 'SearchResult.GetTags()'
更新2:
我还在寻找围绕这一没有成功。我不明白如何与其他属性(例如 EVENTID
)我可以简单地声明数据属性名称为 EVENTID
,它会在表格中显示,但我不能用组合框
列做到这一点。
UPDATE 2:
I'm still searching around for this without success. I don't understand how with other properties (e.g. EventId
) I can simply declare the data property name as EventId
, and it will display in the table, but I cannot do this with ComboBox
columns.
由于对象在一个单独的类实例化,并把在一个列表,它似乎并没有什么意义,我认为我应该有遍历整个数组对象(其中可能有几百个)的标签
属性设置为组合框
列绑定为每个实例,当的我不通过的名单需要循环信息搜索结果
对象绑定其他属性,如: EVENTID
的
Since the objects are instantiated in a separate class and put in a list, it doesn't seem to make sense to me that I should have to loop through the entire array of objects (of which there may be several hundred) to bind the Tags
property to the ComboBox
column for each instance, when I don't need to loop through the list of SearchResult
objects to bind other properties, e.g. EventId
.
为什么这个绑定的属性按名称仅用于工作的有些的性质,而不是别人?
Why does this binding-properties-by-name only work for some properties and not others?
推荐答案
我不明白为什么要使用 DataGridViewComboBoxColumn
显示元素的列表。此列样的设计,让用户选择的许多可能性之一。它接缝它不是你的情况,因为你没有公共字符串SelectedTag {获取;集;}
属性来存储它。据我了解你的模型,你已经选择了你的信息搜索结果
多个标签,并要在网格中显示它们。
I don't quite understand why you want to use DataGridViewComboBoxColumn
to display a list of elements. This column kind is designed to allow user to select one of many possibilities. It seams it is not your case because you don't have public string SelectedTag{get;set;}
property to store it. As I understand your model you have many tags already selected for your SearchResult
and you want to display them in grid.
如文档指出:
获取或设置这个[数据源的]属性获取或设置由CellTemplate属性返回的对象的DataSource属性。设置此属性还设置每一个细胞 DataSource属性列,刷新列显示。要覆盖单个单元格指定的值,您设置列值之后设置单元格值。
DataGridViewComboBoxColumn根本没有能力绑定项属性数据源,因为它假定只有一个被用作用于数据网格中的所有行数据源元素的列表。
DataGridViewComboBoxColumn simply does not have capability to bind items property to data source because it assumes that there is only one list of elements that is used as data source for all rows of data grid.
我还以为你对所有其他的你会设置只读= TRUE
属性此列。如果是这样,将防止标签用户表单看到列表中,因为下拉列表将不会被显示出来。
I also assume that you would set ReadOnly = true
property for this column as you have for all other. If so it would prevent user form seeing list of tags because drop down list would never be displayed.
如果你的魔杖显示只读模式的字符串列表我会建议扁平化的标记,以单个字符串此列表:
If you wand to display list of strings in read only mode I would suggest to flatten this list of tags to single string:
public string Tags { get { return string.Join(", ", tags); } }
和文本栏中显示。
这篇关于如何将DataGridViewComboBoxColumn绑定到返回一个列表对象的属性/方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!