0

I do not understand how to write content from a database to a DataGrid. Here is the code Code DataGrid

<DataGrid AutoGenerateColumns="False" x:Name="ColorsTable" HorizontalGridLinesBrush="DarkGray"
                                        RowBackground="LightGray" AlternatingRowBackground="White" Grid.ColumnSpan="2" Margin="0,0,624.2,0">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="ID" Binding="{Binding Path = Id}" Width="100" />
                                    <DataGridTextColumn Header="Name" Binding="{Binding Path = Name}" Width="100" />
                                    <DataGridTextColumn Header="Specification" Binding="{Binding Path = Specification}" Width="100" />
                                </DataGrid.Columns>
                            </DataGrid>

Code add

ObservableCollection<SomeAbstact> temp = new ObservableCollection<SomeAbstact>();
            sqlCommand = new SqlCommand("SELECT * FROM [Colors]", SqlConnection);
            reader = await sqlCommand.ExecuteReaderAsync();
            while (await reader.ReadAsync())
            {
                temp.Add(new Colors()
                {
                    Id = Convert.ToInt32(reader["Id"]),
                    Name = reader["Name"].ToString(),
                    Specification = reader["Description"].ToString()
                });
            }
            ColorsTable.ItemsSource = temp;
            temp.Clear();
            reader.Close();

Entity add

public class Colors: SomeAbstact
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Specification { get; set; }
}
5
  • 1
    try remove temp.Clear(); and check Commented Apr 2, 2020 at 14:04
  • 1
    temp.Clear(); why are you doing this bro ? you are using this list as ItemSource of DataGrid and after assigning you are Clearing the List ? Don't clear the List. Commented Apr 2, 2020 at 14:05
  • Also Binding="{Binding Path = Id}" instead you can do this Binding="{Binding Id}" Commented Apr 2, 2020 at 14:06
  • Another thing always check for Null when reading data. Commented Apr 2, 2020 at 14:07
  • Thank you the problem was temp.Clear() Commented Apr 2, 2020 at 17:03

1 Answer 1

1

You can bind the DataGrid to the ObservableCollection like this:

<DataGrid ItemsSource="{Binding temp}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Id}"/>
            <DataGridTextColumn Binding="{Binding Name}"/>
            <DataGridTextColumn Binding="{Binding Specification}"/>
        </DataGrid.Columns>
    </DataGrid>

This will only work if the DataContext is set properly. Also you should check out how to implement INotifyPropertyChanged if you want to use bindings in WPF.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.