I have created simple DataGrid with 4 columns, which go outside the bounds of DataGrid, and horizontal scrollbar is not showing. I tried setting width to each column but nothing changed. Here is my XAML:
<Window x:Class="WPFTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<DataGrid Height="200" Width="200" HorizontalScrollBarVisibility="Visible">
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" />
<DataGridTextColumn Header="Column 2" />
<DataGridTextColumn Header="Column 3" />
<DataGridTextColumn Header="Column 4" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
And here is what I get:
I have also tried wrapping DataGrid with ScrollViewer. Nothing. How can I make scrollbar appear?
this was actually a major issue to me since I am creating many columns and allow the user to filter by having filter controls in the header.
When the user scrolls to the right and places a filter in a column and NO rows are returned because of these criteria then the entire grid would move to the left (all columns to the default left position) and the horizontal scrollbar would disappear so the user can't scroll to the filter column to undo his/her action!!
Major pain!
The 'hack' I have just placed in the code is a way around it: (the _dv pointing at a DataView from a DataTable)
This now ensures that the grid stays in its exact position when the no rows are displayed!
The scrolling will work automatically as soon as you add some data to the grid. To test you can apply this code
Name your grid to "grid"
Attached the Window Loaded event
You will see the scrolling appears.
Set
Datagrid
's properties asCanUserAddRows="True" IsReadOnly="True"
and wrap it with ScrollViewer.CanUserAddRows
gives you an empty row. Empty row retains scrollbar.IsReadOnly
hides the empty row since it's readonly.Downside is, when you scroll vertically datagrid headers would be hidden.
Obviously you need horizontal scroll bar.
Two things need to do:
1. Add property
IsReadOnly="True"
to your DataGrid2. Add
MaxWidth="1200" VerticalScrollBarVisibility="Auto"
to yourScrollViewer
MaxWidth need to be set, you can change
1200
to any other value as you need.I think that when the
Datagrid
is empty, theScrollViewer
doesn't handle anything. Try to fill the columns by adding anItemsSource
, the scrolling should therefore appear :)I also very much dislike this behavior, since I use header filters. My "hack" is much easier than the above: simply place the data grid inside a new
ScrollViewer
withHorizontalScrollBarVisibility=Auto
andVerticalScrollBarVisibility=Disabled
(the DataGrid already handles that one fine). Like so:Sure, it's more controls on the page, but a lot easier than the hacky code above. So far I haven't found a way to make the data grid do this automatically.
The side effect of this solution may be the vertical scrollbar hidden until you scroll to the right.