Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the blank column in a WPF DataGrid

Tags:

I use a DataSet to populate a DataGrid in WPF (C#). The result is:

enter image description here

I want to remove blank column at left side. And I want to share remaing space to columns. Expected result is:

enter image description here

My XAML code is:

<Window x:Class="RFID.CareerWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CareerWindow" Height="356" Width="404">
    <Grid>

        <DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="25,10,0,0" VerticalAlignment="Top" Height="306" Width="355" EnableRowVirtualization="false" EnableColumnVirtualization="false" FontFamily="2  badr" FontSize="20" FlowDirection="RightToLeft" CanUserAddRows="False" CanUserReorderColumns="False"/>

    </Grid>
</Window>
like image 690
Babak.Abad Avatar asked Aug 13 '13 17:08

Babak.Abad


2 Answers

Avoid setting static Height and Width.

Use ColumnWidth="*" to share the space between your DataGridColumns

<DataGrid x:Name="dg1" ColumnWidth="*"
          HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25,10,0,0"
          EnableRowVirtualization="false" EnableColumnVirtualization="false" 
          FontFamily="2  badr" FontSize="20" FlowDirection="RightToLeft" 
          CanUserAddRows="False" CanUserReorderColumns="False" />
like image 175
Nitesh Avatar answered Sep 20 '22 06:09

Nitesh


You can set the last column or one column of your Grid with

<DataGridTextColumn Header="Surname"
                    Width="*"
                    Binding="{Binding Path=Surname,Mode=TwoWay}" IsReadOnly="True">
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="TextBlock">
      <Setter Property="HorizontalAlignment" Value="Left" />
      <Setter Property="VerticalAlignment" Value="Center" />
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
like image 25
daniele3004 Avatar answered Sep 19 '22 06:09

daniele3004