Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. DataGrid displays lengths of strings instead of the actual strings

DataGrid displays lengths of strings instead of the actual strings

Scheduled Pinned Locked Moved WPF
csharpcsswpfcomquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nick Alexeev
    wrote on last edited by
    #1

    Colleagues, Familiarizing myself with DataGrid (and with C# in general). My DataGrid is declared like this:

    <my:DataGrid AutoGenerateColumns="True" Margin="74,32,68,103" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />

    I’m setting the ItemSource property equal to a list of strings.

    public Window1()
    {
    InitializeComponent();

    List<string> lstGridData = new List<string>();
    for (int i = 0; i < 8; ++i) lstGridData.Add("str");
    dataGrid1.ItemsSource = lstGridData;
    

    }

    I’m expecting that grid would display one column and each line would show “str”. Instead, the grid displays one column titled “Length” and each line displays “3”. What am I missing? Cheers, - Nick

    M 1 Reply Last reply
    0
    • N Nick Alexeev

      Colleagues, Familiarizing myself with DataGrid (and with C# in general). My DataGrid is declared like this:

      <my:DataGrid AutoGenerateColumns="True" Margin="74,32,68,103" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />

      I’m setting the ItemSource property equal to a list of strings.

      public Window1()
      {
      InitializeComponent();

      List<string> lstGridData = new List<string>();
      for (int i = 0; i < 8; ++i) lstGridData.Add("str");
      dataGrid1.ItemsSource = lstGridData;
      

      }

      I’m expecting that grid would display one column and each line would show “str”. Instead, the grid displays one column titled “Length” and each line displays “3”. What am I missing? Cheers, - Nick

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      The AutoGenerateColumns mechanism binds to any bindable properties it finds in the objects in the List. For a string, that's String.Length.

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      N 1 Reply Last reply
      0
      • M Mark Salsbery

        The AutoGenerateColumns mechanism binds to any bindable properties it finds in the objects in the List. For a string, that's String.Length.

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        N Offline
        N Offline
        Nick Alexeev
        wrote on last edited by
        #3

        How can I make a "bindable string"? Is there a framework object with a bindable string property which is commonly used for this purpose? May be, one option is to make a wrapper object with a bindable string property (call it, say, BindableString). Is there a simple/quick solution to "populate DataGrid with primitive values" problem? Tried with manually defined columns (AutoGenerateColumns=False). Didn't work either. Looking for a good DataGrid tutorial... Cheers, - Nick

        modified on Monday, October 19, 2009 2:26 AM

        M 1 Reply Last reply
        0
        • N Nick Alexeev

          How can I make a "bindable string"? Is there a framework object with a bindable string property which is commonly used for this purpose? May be, one option is to make a wrapper object with a bindable string property (call it, say, BindableString). Is there a simple/quick solution to "populate DataGrid with primitive values" problem? Tried with manually defined columns (AutoGenerateColumns=False). Didn't work either. Looking for a good DataGrid tutorial... Cheers, - Nick

          modified on Monday, October 19, 2009 2:26 AM

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Nick Alexeev wrote:

          Is there a simple/quick solution to "populate DataGrid with primitive values" problem?

          Manually bind to the object itself...

          <my:DataGrid AutoGenerateColumns="False" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" >
              <my:DataGrid.Columns >
                  <my:DataGridTextColumn Header="String" Binding="{Binding Path=.}" />
              </my:DataGrid.Columns>
          </my:DataGrid>
          

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups