2 Replies - 9182 Views - Last Post: 25 May 2013 - 12:15 PM Rate Topic: -----

#1 johnyjj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 24-July 09

[WPF] list of string and binding to data grid

Posted 23 May 2013 - 10:19 AM

Hello,

I would like to bind list of string values to datagrid. I have such a problem that one time I only receive length values of string (because string contains only one public property, i.e. length), and the other time datagrid without any values.

How can I bind those values to datagrid? I have List<List<string>>, where inner list is with values for given row, and outter list contains all the rows.

Xaml file contains:

<DataGrid Grid.Row="0" Grid.Column="0" Margin="5" Name="dgrComparison" ItemsSource="{Binding}">
 
</DataGrid>


Xaml.cs file contains:

        /*
        /// <summary>We use this class because binding list of strings
        /// to data grid gives string length instead of string value.</summary>
        public class StringValue
        {
            // http://stackoverflow.com/questions/479329/how-to-bind-a-string-list-to-a-datagrid
            public StringValue(string s)
            {
                _value = s;
            }
            public string Value { get { return _value; } set { _value = value; } }
            string _value;
        }
        */
 
        public List<List<string>> summaryRows;
 
        public Visualization()
        {
                InitializeComponent();
 
                // Create new list with values used in summary
                summaryRows = new List<List<string>>();
                #region Fill summaryRows with test data
                // Test data
                List<string> testRow = new List<string>();
                testRow.Add("first");
                testRow.Add("second");
                summaryRows.Add(testRow);
                List<string> testRow2 = new List<string>();
                testRow2.Add("third");
                testRow2.Add("fourth");
                summaryRows.Add(testRow2);
                #endregion Fill summaryRows with test data
                dgrComparison.DataContext = summaryRows;
                dgrComparison.ItemsSource = summaryRows.ToList().Select(x => new { x }); ;
        }


What haven't I taken into account in this code, so that I can bind string values from List<List<string>> summaryRows to data grid dgrComparison?

Best regards!

Is This A Good Question/Topic? 0
  • +

Replies To: [WPF] list of string and binding to data grid

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: [WPF] list of string and binding to data grid

Posted 23 May 2013 - 12:37 PM

Moved to WPF
Was This Post Helpful? 0
  • +
  • -

#3 johnyjj2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 24-July 09

Re: [WPF] list of string and binding to data grid

Posted 25 May 2013 - 12:15 PM

Hello,

how can I accomplish my goal, i.e. bind List<List<string>> to data grid?

For example, can I do it by setting RowTemplate and using ListView to use my internal list? (Because DataGrid may want to always accept my data as a list and display one list after the other). If yes, how can it be encoded, as my knowledge about WPF is rather limited?

Thanks in advance for help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1