23

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>
rene
  • 41,474
  • 78
  • 114
  • 152
Babak.Abad
  • 2,839
  • 10
  • 40
  • 74

3 Answers3

49

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" />
Nitesh
  • 7,261
  • 3
  • 30
  • 25
  • It works perfectly! Thank you! Now, can you help me align all text (cells and header) as center? – Babak.Abad Aug 13 '13 at 18:20
  • 2
    Please check this SO post to align cell text. http://stackoverflow.com/questions/3652318/datagrid-text-alignment – Nitesh Aug 13 '13 at 18:32
  • 1
    This SO post to align header content. http://stackoverflow.com/questions/8810507/how-to-right-align-text-in-a-datagrid-column-header-in-xaml – Nitesh Aug 13 '13 at 18:33
  • 1
    `HorizontalAlignment="Left"` is what did it for me. – jsirr13 Dec 14 '21 at 21:30
  • I second @jsirr13, setting the alignment worked, where setting the width shrank all my columns to a few characters wide – Steve Mar 09 '22 at 16:39
3

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>
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
daniele3004
  • 13,072
  • 12
  • 67
  • 75
0

To eliminate an automatic added column, set Width of a DataGridTextColumn to *

The following Code represents a DataGrid with two columns and no automatic added colums

<DataGrid AutoGenerateColumns="False" HeadersVisibility="Column">
 <DataGrid.Columns>
  <DataGridTextColumn Width="*" Binding="{Binding asdf}" />
  <DataGridTextColumn Width="*" Binding="{Binding zuio}" />     
 </DataGrid.Columns>
</DataGrid>
Fluckzeug
  • 1
  • 2