0

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:

<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?

  • You should have seen data binding error messages in the Output Window in Visual Studio when you are running your application in the debugger. – Clemens Oct 20 '22 at 13:47

1 Answers1

0

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;}
}
Oliver Weichhold
  • 10,259
  • 5
  • 45
  • 87