0

I'm creating a C# project where many columns of Combo Box are added programmatically in a datagrid. Unfortunately when adding many columns, the datagrid is not able to display them all, and the horizontal scroll bar is disabled and unclickable..

Design :

<DataGrid ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible"  x:Name="dg" Grid.Column="1" Grid.Row="0" AutoGenerateColumns="false"  Background="#FFDFF9F9" Height="76" VerticalAlignment="Top" Margin="2,82,0,0" HorizontalAlignment="Stretch"/>

Code :

        foreach (var r in importMappings)
        {
            var dgtc = new DataGridTextColumn();
            dgtc.Binding = new Binding(string.Format("[{0}]", r.Key));
            var sp = new StackPanel();
            dgtc.Header = sp;
            sp.Children.Add(new Label { Content = r.Key });
            var combo = new ComboBox();
            sp.Children.Add(combo);
            combo.ItemsSource = excelHeaders;
            int x = combo.SelectedIndex;
            var selectedBinding = new Binding(string.Format("[{0}]", r.Key));
            selectedBinding.Source = importMappings;
            combo.SetBinding(Selector.SelectedIndexProperty, selectedBinding);
            dgtc.CanUserSort = false;
            dgtc.CanUserReorder = false;
            dg.Columns.Add(dgtc);
        }

Live View :

enter image description here

The scroll bar is always disabled here. Any ideas on how to make the scroll bar work ?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Salwa Najm
  • 47
  • 5

1 Answers1

1

It looks like you don't have any data row. After adding a row of data the scrollbar worked for me.

For testing just edit your datagrid to:

<DataGrid ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible"  x:Name="dg" Grid.Column="1" Grid.Row="0" AutoGenerateColumns="false"  Background="#FFDFF9F9" Height="76" VerticalAlignment="Top" Margin="2,82,0,0" HorizontalAlignment="Stretch">
<TextBlock />
</DataGrid>

The scrollbar should be working now.

Steven S.
  • 724
  • 7
  • 10