Sandeep Bansal Sandeep Bansal - 28 days ago 22
C# Question

Manually add rows in WPF DataGrid

I have the following XAML Code:

<sdk:DataGrid Margin="58,8,52,18" Name="dataGridTickets">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="ticketNoColumn" Header="Ticket No." IsReadOnly="True" Width="SizeToHeader"/>
<sdk:DataGridTextColumn x:Name="seatRowColumn" Header="Seat Row" IsReadOnly="True" Width="SizeToHeader"/>
<sdk:DataGridTextColumn x:Name="seatNumberColumn" Header="Seat Number" IsReadOnly="True" Width="SizeToHeader"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>


I would like to enter manual data into the grid programatically, how can I manage to do this?

Thanks

Working Solution

Programatically add rows in a WPF DataGrid

Answer

You don't add rows to a grid.

  1. Bind the Grid to a List (Observable collection)
  2. Add items to that list.

Result: new rows show up in the grid.