4

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;
    }
}

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;

I derived this code from a tutorial I found online, and it works perfectly.

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.

  1. According to the documentation and the linked question, DisplayMember should be linked to the property that "contains a description of the instance", but since SearchResult objects are added dynamically and don't have any description associated with them, should I just leave it blank?
  2. ValueMember is giving me similar problems, since I'm unsure what to put even after reading its documentation.
  3. 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

I also presume I should have a line that sets the DataSource for the column, e.g.

tagsColumn.DataSource = <some LINQ query, perhaps?>

but I don't know because the only mostly relevant C# source I've been able to find is that question.

UPDATE:

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

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 = ""; // ??

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()'

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.

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?

Community
  • 1
  • 1
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
  • 1
    The error arises from you calling SearchResult.GetTags(); You'll need to either make this a static method, or instantiate SearchResult first (new...) – blearn Jun 29 '12 at 20:38
  • @blearn In the answer to the second linked question, the object wasn't instantiated. Also, since `SearchResult` objects will be added to this list dynamically, in a method in a separate class, I won't necessarily have access to them unless I iterate through them, in which case I might as well just skip data binding and populate the `DataGridView` manually, which I'm trying to avoid doing. – Ricardo Altamirano Jun 29 '12 at 20:42

2 Answers2

3

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.

As documentation states: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource

Getting or setting this [DataSource] property gets or sets the DataSource property of the object returned by the CellTemplate property. Setting this property also sets the DataSource property of every cell in the column and refreshes the column display. To override the specified value for individual cells, set the cell values after you set the column value.

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.

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); } }

and display it in text column.

Rafal
  • 12,391
  • 32
  • 54
  • I originally wanted to do this because I wasn't sure of another way, but I did end up using something similar to the code you posted (using `Join`) and creating my own user control. – Ricardo Altamirano Jul 05 '12 at 13:20
  • Sorry for the super late comment but if you are still around, I have a list of objects similar to the OP that do have a "SelectedTag" and "AvailableTags" properties and want to give each row it's own "AvailableTags" list with different options. Is there any way to do this using the designer/without manually binding everything? – Marie Oct 04 '18 at 21:45
1

For the error , i can suggest you to make an instance of the class and then call the method as its not static or you can make your method static.

Moreover As you needs the comboboxcolumn ,

DataGridViewComboBoxColumn tagsColumn = new DataGridViewComboBoxColumn();
        tagsColumn.DataSource = SearchResult.GetTags(); // ERROR 
        tagsColumn.DisplayMember = ""; // Still not sure
        tagsColumn.ValueMember = ""; // ??

Mostly we have dropdowns for objects like Country(id,name) so DisplayMember = name will be shown as text in dropdown while ValueMember = id will be used in the referencing tables in database.But this is not your case.

Here you have a list of strings to show in dropdown , so you don't need to set them. As written here

If the DataSource property is set to a string array, then ValueMember and DisplayMember do not need to be set because each string in the array will be used for both value and display.

abhinav pratap
  • 623
  • 8
  • 15
  • Is there no other way to do this than use an instance of the object? I can't just do it automatically as I can with the other properties? See my comment on the original question. – Ricardo Altamirano Jun 29 '12 at 21:14