Search code examples
c#wpfwpfdatagrid

How to fill a DataGrid from String Array?


So I'm currently creating a C# WPF application with a DataGrid to show the user some strings he entered before. I'm pretty new to WPF and programming in general so I have no clue where to begin. My problem is that I don't know how to fill data in the DataGrid. This could be a duplicate but as there aren't simple tutorials for beginners I would be happy about some help.

I currently have an ArrayList userInput filled with strings:

userInput{"the", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}

This is the result I want to achieve:

(1) the   | brown
(2) fox   | jumps
(3) over  | the
(4) lazy  | dog

I did some research and I found something named data-binding but I don't really understand how it works. Thank you for your help in advance.


Solution

  • I can suggest the solution for you in a simpler way. you must have schema first. simply need to convert your array list to an CustomClass list

        public class User
        {
            public string Name { get; set; }
        }
    
    
    
    
      public List<User> users = new List<User>();
    
      users.Add(new User() { Name = "dogs" });
      users.Add(new User() { Name = "dog" });
      users.Add(new User() { Name = "cat" });
      users.Add(new User() { Name = "cats" });
    
    
     this.dataGrid1.ItemsSource = users;
    
      <DataGrid Height="179" HorizontalAlignment="Left" Margin="54,65,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="382">
            </DataGrid>