Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my binded Observeble Collection to my DataGrid only showing empty rows?

I have an ObservableCollection:

        public ObservableCollection<FieldsDataGrid> ocEingaben = new ObservableCollection<FieldsDataGrid>();

And my Fields:

public class FieldsDataGrid
{
    public string Teil;
    public decimal Preis;
}

now I binded it to my DataGrid like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<DataGrid  Style="{DynamicResource DataGridStyle}" 
                       Margin="10"  SelectionUnit="CellOrRowHeader"  AutoGenerateColumns="False" 
                       CanUserAddRows="True" CanUserDeleteRows="True" SelectionMode="Single" 
                       x:Name="IDGrid" ItemsSource="{Binding ocEingaben}"
                       GridLinesVisibility="None"  ScrollViewer.CanContentScroll="True" 
                       ScrollViewer.VerticalScrollBarVisibility="Auto" MaxHeight="380"
                       ScrollViewer.HorizontalScrollBarVisibility="Auto">
                        <DataGrid.Columns >
                            <DataGridTextColumn  Header="Teil"  Binding="{Binding Titel}" Width="*"/>
                            <DataGridTextColumn  Header="Preis"  Binding="{Binding Preis}" Width="*"/>
                        </DataGrid.Columns>
                    </DataGrid>

I want it like that, that I type Excel Columns in an TextBox and the "Teil" Column will update with data from the Excel, but that Is not relevant for this Question.

Because when Im testing only the function of adding input after the Text Input it not really work.

Keydown event of my TextBox Input:

 if (e.Key == Key.Return)
        {
            

            View.editotherList();
            IDGrid.ItemsSource = View.ocEingaben;
            InputCell.Clear();


        }

ocEingaben is my Collection and in that Method I add data to the Collection with this test data:

            ocEingaben.Add(new FieldsDataGrid() { Teil = "Zellenwert", Preis = 0.0m });

but after I made the Input I get the number of the rows but the Values are not displaying:

Output

Anyone an Idea?

>Solution :

First problem is your binding refers to Titel property but the object only contains a Teil field. Second problem is that your object defines fields instead of properties like this:

public class FieldsDataGrid
{
    public string Titel {get; set;}
    public decimal Preis {get; set;}
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading