Fill the DataGrid with empty rows

I have a DataGrid related to a DataTable . I want the DataGrid always have at least ten (empty) rows, also if there are not enough real data elements (the data comes in a bit).
Example

One approach is to easily add ten empty rows to a DataTable during initialization. But when the real data item arrives, I can’t easily add the line, I need to find the first empty line to overwrite it, which is not very convenient.
So does anyone know a smarter / built-in way to achieve this?

+6
source share
2 answers

It will be a mess, no matter which side he approached. I would say that your best bet (assuming the contents of the grid cell is not wrapped) is to use a visual brush (with lines) as the background of the DataGrid.

UPDATE 1 - XAML There are many, the trick is to use MinHeight, which will create a vision of empty objects thanks to the tiled background. If your grid is filled with real data, the background will expand, creating more lines.

One thing I haven't tried is how it will handle scrolling.

Here is an example:

 <Grid> <Grid.Resources> <VisualBrush x:Key="StripesBrush" TileMode="Tile" Viewport="0,0,5,20" Viewbox="0,0,10,10" ViewportUnits="Absolute" ViewboxUnits="Absolute"> <VisualBrush.Visual> <Line X1="0" X2="10000" Y1="0" Y2="0" Stroke="DarkGray"/> </VisualBrush.Visual> </VisualBrush> </Grid.Resources> <DataGrid x:Name="g" AutoGenerateColumns="False" GridLinesVisibility="None" MinHeight="100" Height="100" VerticalAlignment="Top" Background="{StaticResource StripesBrush}"> <DataGrid.Columns> <DataGridTextColumn Header="A" Width="Auto" Binding="{Binding Item1}"> <DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="Background" Value="Transparent"></Setter> </Style> </DataGridTextColumn.CellStyle> </DataGridTextColumn> <DataGridTextColumn Header="B" Width="Auto" Binding="{Binding Item2}" /> </DataGrid.Columns> </DataGrid> </Grid> 

0
source

if it's just for layout, you can just add a second dataset with 10 blank rows under your real datagrid. or you take aproach Dmitry and use a visual brush.

0
source

Source: https://habr.com/ru/post/902288/


All Articles