rockstar
rockstar

Reputation: 3538

C# Datagrid has extra redundant column

enter image description hereI have a strange problem while trying to use Datagrids in C# . An extra column is appened to my datagrid which does not look neat .

I followed the tutorials that i always do DataGrid in WPF . I also downloaded the example and executed them but as i see in this example as well an extra column is appended . ( Figure 2 & Figure 7 )

Why is this so ? can i get rid of this redundant column ?

Left Panel :

    <DockPanel >
        <TextBox DockPanel.Dock="Top" Text="Unsecured Devices" IsReadOnly="True"/>
        <DataGrid x:Name="unSecure" ItemsSource="{Binding UnsecuredDevices}"/>
    </DockPanel>

Right Panel :

    <DockPanel  Grid.Column="2">
        <TextBox DockPanel.Dock="Top" Text="Secured Devices" IsReadOnly="True" />
        <DataGrid x:Name="Secure" ItemsSource="{Binding SecuredDevices}" />
    </DockPanel>

Upvotes: 0

Views: 1685

Answers (3)

Damith
Damith

Reputation: 63065

set ColumnWidth ="*" Space is unused space of datagrid. you need to set column width to fill the space.

<DockPanel >
   <TextBox DockPanel.Dock="Top" Text="Unsecured Devices" IsReadOnly="True"/>
   <DataGrid ColumnWidth ="*" x:Name="unSecure" ItemsSource="{Binding UnsecuredDevices}"/>
</DockPanel>

Upvotes: 2

sa_ddam213
sa_ddam213

Reputation: 43596

Its not an empty column its just empty space left over, You can fix this by setting the ColumnWidth on your DataGrids

Auto The unit of measure is based on the size of the cells and the column header.

Pixel The unit of measure is expressed in pixels.

SizeToCells The unit of measure is based on the size of the cells.

SizeToHeader The unit of measure is based on the size of the column header.

Star The unit of measure is a weighted proportion of the available space.

I think Star(*) will work for your layout <DataGrid ColumnWidth="*">

Upvotes: 2

İsmet Alkan
İsmet Alkan

Reputation: 5447

The tutorial itself has an extra column as you said. You can adjust height and width, just for saving the day. Such as:

<DataGrid x:Name="Secure" ItemsSource="{Binding SecuredDevices}" 
          Height="200" Width="500" HorizontalAlignment="Left" Margin="12,21,0,0"
          VerticalAlignment="Top" RowHeight="30" ColumnWidth="100" />

Upvotes: 1

Related Questions