If the column does not have DisplayMemberBinding or CellTemplate , then ToString is called for each element in the grid, and the evaluated value is used as the contents of the cell.
For example, if you have a class:
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return LastName; } }
If you bind the Person collection to a grid view:
var people = new List<Person> { new Person { FirstName = "Peter", LastName = "Smith" }, new Person { FirstName = "Steve", LastName = "White" }, new Person { FirstName = "Dave", LastName = "Gates" }, }; listView1.ItemsSource = people;
And if you create a new column without binding, LastName will be shown in this column, because the overridden ToString() returns LastName . If you modify ToString to return FirstName , then FirstName appears in the new column.
I hope I understood your question correctly.
[change]
If you add many new columns and do not use DisplayMemberBinding , then all new columns will evaluate and display the result of ToString() . You need to set DisplayMemberBinding to display a specific property.
var newColumn = new GridViewColumn(); newColumn.Header = "Test"; newColumn.DisplayMemberBinding = new Binding("FirstName"); gridView.Columns.Add(newColumn);
[edit] Answers to your questions:
When ToString () is called on each element (which represents a row), how are those displayed on the View grid columns? How to change an element in a row so that the new column is filled?
ToString is called when the cell's presenter is displayed, unless you specify a binding or template that will also use a binding at the end. You cannot modify a row element to control how it appears in the grid unless you override ToString or column-bound properties.
Not sure if I fully understand your problem. You are adding a new column, right? Why don't you set the DisplayMemberBinding property in your column? If you do this, you can display whatever you want in the column.
[change]
I think I answered your question about generosity :). I think I have already earned generosity :).
However, here is the answer to your next question:
How would I set DataMemberBinding to achieve a mapping to ListRow.Items [5]?
Answer:
newColumn.DisplayMemberBinding = new Binding("Items[5]");