Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent WPF 4.0 Datagrid from Showing Empty Column

alt text

I have an application with a datagrid with 1 column (for now). How do I remove the second, empty column from the datagrid such that only columns with data are displayed in the datagrid.

like image 852
Kiang Teng Avatar asked Jan 07 '11 16:01

Kiang Teng


3 Answers

As vorrtex said in a comment the best thing to do is probably to set the column width to fill all available space:

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="FishLine ID" Width="*"/>
    </DataGrid.Columns>
    ...
</DataGrid>

Depending on the container you use you could also align the grid to the left side, leaving empty space to its right:

<DataGrid HorizontalAlignment="Left">
    <DataGrid.Columns>
        <DataGridTextColumn Header="FishLine ID"/>
    </DataGrid.Columns>
    ...
</DataGrid>

Hopefully that is what you were looking for...

like image 187
H.B. Avatar answered Oct 29 '22 18:10

H.B.


I think you need to set AutoGenerateColumns to False, and do something like this:

<DataGrid AutoGenerateColumns = "False" ItemsSource = "{Binding BindSource}">
    <DataGrid.Columns>
        <DataGridTextColumn Header = "FishLine ID" Binding = "{Binding ID}" Width="*"/>
    </DataGrid.Columns>
</DataGrid>

That should do it :p

like image 37
Machinarius Avatar answered Oct 29 '22 18:10

Machinarius


If width of one column is "*" even then I've seen empty column at right of datagrid. To solve this, specify the Width of datagrid as "Width=500" instead of MinWidth and MaxWidth.

<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="500" MinHeight="180" CanUserAddRows="False" CanUserDeleteRows="false" ItemsSource="{Binding MyList}" SelectedItem="{Binding SelectedValue}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Start Date" Binding="{Binding StartDate}" IsReadOnly="True" MinWidth="60" Width="Auto"/>
        <DataGridTextColumn Header="End Date" Binding="{Binding EndDate}" IsReadOnly="True" MinWidth="60" Width="*"/>
    </DataGrid.Columns>
</DataGrid>
like image 2
Omer Avatar answered Oct 29 '22 16:10

Omer