Tkay
Tkay

Reputation: 111

C# WPF DataGrid | Only one Row without Content

I tried a little bit with the Datagrid, in the first Programm I've created an DataGrid, which worked very well but then I tried another Project, to redo it and Learn it without Guide.

I dont know what there is Diffrent to the other Project but this Time it shows only 1 Row, after clicking on ADD, without Content.

What did I wrong and are there generaly Improvements?

Thanks for use you Time in this Thread.

C# MainClass:

    public List<User> UserList = new List<User>();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void PressedEnter(object sender, KeyEventArgs e)
    {
        TextBox CurrentTextbox = (TextBox)sender;

        if (e.Key == Key.Enter)
        {
            switch (CurrentTextbox.Name)
            {
                case "EnterName":
                    EnterBirthday.Focus();
                    break;

                case "EnterBirthday":
                    EnterEmail.Focus();
                    break;

                case "EnterEmail":
                    MainDataGrid.ItemsSource = null;
                    MainDataGrid.ItemsSource = UserList;
                    break;
            }
        }
    }

    private void AddButton_OnClick(object sender, MouseButtonEventArgs e)
    {
        MainDataGrid.ItemsSource = null;
        MainDataGrid.ItemsSource = UserList;
    }

    private List<User> AddItemToGrid()
    {
        string[] NameArray = EnterName.Text.Split(' ');
        string[] BirthdayArray = EnterBirthday.Text.Split('.');

        UserList.Add(new User()
        {
            FirstName = NameArray[0],
            LastName = NameArray[1],
            Birthday = new DateTime(Convert.ToInt16(BirthdayArray[2]), Convert.ToInt16(BirthdayArray[1]), Convert.ToInt16(BirthdayArray[0])),
            Email = EnterEmail.Text
        });

        return UserList;
    }

C# UserClass

public class User
{
    public string FirstName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public DateTime Birthday
    {
        get;
        set;
    }

    public string Email
    {
        get;
        set;
    }
}

XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="287*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="235" />
        <ColumnDefinition Width="337*" />
    </Grid.ColumnDefinitions>

    <Label Grid.ColumnSpan="2"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        Background="#2196f3"
        BorderBrush="Transparent"
        Content="DataGrid"
        FontFamily="Roboto"
        FontSize="15"
        FontWeight="Bold"
        Foreground="White" />
    <StackPanel Grid.Row="1" Grid.Column="0">
        <Label Height="25"
            Margin="10,10,10,0"
            VerticalAlignment="Top"
            HorizontalContentAlignment="Center"
            Background="#2196f3"
            BorderBrush="Transparent"
            Content="Name"
            FontFamily="Roboto"
            FontSize="13"
            FontWeight="Bold"
            Foreground="White" />
        <TextBox Name="EnterName"
                Height="25"
                Margin="10,5,10,0"
                VerticalAlignment="Top"
                HorizontalContentAlignment="Left"
                VerticalContentAlignment="Center"
                BorderBrush="#2196f3"
                FontFamily="Roboto"
                Foreground="#2196f3"
                KeyDown="PressedEnter" />

        <Label Height="25"
            Margin="10,10,10,0"
            VerticalAlignment="Top"
            HorizontalContentAlignment="Center"
            Background="#2196f3"
            BorderBrush="Transparent"
            Content="Birthday (DD.MM.YYYY)"
            FontFamily="Roboto"
            FontSize="13"
            FontWeight="Bold"
            Foreground="White" />
        <TextBox Name="EnterBirthday"
                Height="25"
                Margin="10,5,10,0"
                VerticalAlignment="Top"
                HorizontalContentAlignment="Left"
                VerticalContentAlignment="Center"
                BorderBrush="#2196f3"
                FontFamily="Roboto"
                Foreground="#2196f3"
                KeyDown="PressedEnter"
                MaxLength="10" />

        <Label Height="25"
            Margin="10,10,10,0"
            VerticalAlignment="Top"
            HorizontalContentAlignment="Center"
            Background="#2196f3"
            BorderBrush="Transparent"
            Content="E-Mail"
            FontFamily="Roboto"
            FontSize="13"
            FontWeight="Bold"
            Foreground="White" />
        <TextBox Name="EnterEmail"
                Height="25"
                Margin="10,5,10,0"
                VerticalAlignment="Top"
                HorizontalContentAlignment="Left"
                VerticalContentAlignment="Center"
                BorderBrush="#2196f3"
                FontFamily="Roboto"
                Foreground="#2196f3"
                KeyDown="PressedEnter" />
        <Label Name="AddButton"
            Height="68"
            Margin="10,10,10,10"
            VerticalAlignment="Stretch"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            Background="#2196f3"
            BorderBrush="#2196f3"
            Content="ADD"
            FontFamily="Roboto"
            FontSize="30"
            FontWeight="Bold"
            Foreground="White"
            MouseLeftButtonDown="AddButton_OnClick" />
    </StackPanel>
    <DataGrid Name="MainDataGrid"
            Grid.Row="1"
            Grid.Column="1" />
</Grid>

Upvotes: 2

Views: 1274

Answers (1)

Bahman_Aries
Bahman_Aries

Reputation: 4798

You forgot to set UserList:

    private void AddButton_OnClick(object sender, MouseButtonEventArgs e)
    {
        MainDataGrid.ItemsSource = null;
        UserList = AddItemToGrid();
        MainDataGrid.ItemsSource = UserList;
    }

Upvotes: 2

Related Questions