Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If column doesn't have <code>DisplayMemberBinding</code> or <code>CellTemplate</code> then ToString is called on each item in the grid and evaluated value is used as cell content.</p> <p>For example, if you have class:</p> <pre><code>public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return LastName; } } </code></pre> <p>If you bind a collection of <code>Person</code> to the grid view:</p> <pre><code>var people = new List&lt;Person&gt; { new Person { FirstName = "Peter", LastName = "Smith" }, new Person { FirstName = "Steve", LastName = "White" }, new Person { FirstName = "Dave", LastName = "Gates" }, }; listView1.ItemsSource = people; </code></pre> <p>And if you create a new column without binding, then LastName will be shown in this column, because overridden <code>ToString()</code> returns <code>LastName</code>. If you change <code>ToString</code> to return <code>FirstName</code> then new column will show <code>FirstName</code>.</p> <p>Hope I understood your question correctly.</p> <p><strong>[Edit]</strong></p> <p>If you add many new columns and don't use <code>DisplayMemberBinding</code> then all the new columns will evaluate and show <code>ToString()</code> result. You need to set <code>DisplayMemberBinding</code> to show particular property.</p> <pre><code>var newColumn = new GridViewColumn(); newColumn.Header = "Test"; newColumn.DisplayMemberBinding = new Binding("FirstName"); gridView.Columns.Add(newColumn); </code></pre> <p><strong>[Edit]</strong> Answers to your questions:</p> <blockquote> <p>When ToString() is called on every item (which represents a row), how is that mapped towards the columns of the GridView? How must I alter an item in the row so that the new column is filled?</p> </blockquote> <p>ToString is called when cell presenter is rendered, unless you specified binding or template, which would also use binding in the end. You cannot alter row item to control how it is represented in the grid, unless you override ToString or properties that are bound to the columns.</p> <p>Not sure I understand your problem completely. You are adding a new column, right? Why don't you set DisplayMemberBinding property on your column? If you do this you will be able to show in the column whatever you want.</p> <p><strong>[Edit]</strong></p> <p>I think I answered your bounty question :). I think I already earned the bounty :).</p> <p>However, here is the answer to your next question:</p> <blockquote> <blockquote> <p>How would I set DataMemberBinding to achieve mapping to ListRow.Items[5]?</p> </blockquote> </blockquote> <p>The answer:</p> <pre><code>newColumn.DisplayMemberBinding = new Binding("Items[5]"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload