我有一個自定義對象與幾個屬性,其中之一返回一個列表。這是該對象的代碼:如何將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',我試圖避免這麼做。 –