Home > Blockchain >  Adding new rows to a DataGrid appears blank
Adding new rows to a DataGrid appears blank

Time:10-14

I'm trying to add new rows to a DataGrid, but when I'm adding them they appear blank.

string[] test = { "hi", "bye" };
this.MessageDataGrid.Items.Add(test);
<Grid>
    <DataGrid x:Name="MessageDataGrid">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Naam" />
            <DataGridTextColumn Header="Bericht"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

Blank row after adding an item to the DataGrid.

CodePudding user response:

Each item in your DataGrid is an array of string with two elements. Use a Binding with an indexer (square brackets [ ]) to refer to each element. See Binding path syntax for more information.

<DataGrid x:Name="MessageDataGrid">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Naam" Binding="{Binding [0]}"/>
      <DataGridTextColumn Header="Bericht" Binding="{Binding [1]}"/>
   </DataGrid.Columns>
</DataGrid>
  • Related