Bonjour,

Je souhaite binder une listbox à un datagridview

La listbox fait référence à une table: TABLE_CHANTIER
Le datagridview fait référence à une table TABLE_TOUPIE (c'est des camions de livraison de béton pour info...)

Chaque table contient un champ NUM_CAMION

Je voudrais donc que quand je clique sur un chantier dans la listbox, la liste des camions associés apparaisse dans le datagridview...

Le problème est que je ne sait pas comment faire pour remplir mon dataset correctement...

Code behind:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    Private Sub TabGestion_Chantier_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TabGestion_Chantier.Loaded
        'strConn = "provider=microsoft.jet.oledb.4.0;" & "Data Source= c:\base.mdb;"
        'strConn = "provider=microsoft.jet.oledb.4.0;" & "Data Source= " & AppDomain.CurrentDomain.BaseDirectory & "\base.mdb;"
        strConn = "provider=microsoft.jet.oledb.4.0;" & "Data Source= " & My.Application.Info.DirectoryPath & "\base.mdb;"
 
        'strSql = "SELECT TABLE_CHANTIER.* FROM TABLE_CHANTIER"
        strSql = "SELECT * FROM TABLE_CHANTIER"
 
        ObjetConnection = New OleDbConnection
        ObjetConnection.ConnectionString = strConn
        ObjetConnection.Open()
 
        ObjetCommand = New OleDbCommand(strSql)
        ObjetDataAdapter = New OleDbDataAdapter(ObjetCommand)
        ObjetCommand.Connection() = ObjetConnection
        ObjetDataAdapter.Fill(ObjetDataSet, "TABLE_CHANTIER")
        DataContext = ObjetDataSet
 
    End Sub
Code XAML

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window1" Height="500" Width="800" WindowState="maximized" >
 
    <Window.Resources>
        <Style x:Key="Lecture_Seule" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=_CheckBox_Lecture_Seule, Path=IsChecked}" Value="True">
                    <Setter Property="TextBox.IsEnabled" Value="False"/>
                    <Setter Property="TextBox.BorderThickness" Value="0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=_CheckBox_Lecture_Seule, Path=IsChecked}" Value="False">
                    <Setter Property="TextBox.IsEnabled" Value="True"/>
                    <Setter Property="TextBox.BorderThickness" Value="2"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
 
        <DataTemplate x:Key="Mise_en_forme_liste">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition Height="10"/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Path=NOM_CHANTIER}" Grid.Column="0" FontWeight="bold" />
                <TextBlock Name="_TextBlock1" Text="{Binding Path=NOM_CLIENT}" Grid.Row="1"  FontSize="9"/>
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NOM_CLIENT}" Value="EBN PERIERS">
                    <Setter TargetName="_TextBlock1" Property="Foreground" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=NOM_CLIENT}" Value="ROL NORMANDIE GRAN.">
                    <Setter TargetName="_TextBlock1" Property="Foreground" Value="Blue" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>
 
    <Grid>
        <TabControl Margin="3,3,3,3" Name="TabControl_Principal" >
            <TabItem Header="Gestion Chantier" Name="TabGestion_Chantier">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="250" />
                        <ColumnDefinition Width="7" />
                        <ColumnDefinition  />    
                    </Grid.ColumnDefinitions>
                    <ListBox Grid.Column="0" 
                             Margin="14,28,14,10" 
                             Name="ListBox1"
                             ItemsSource="{Binding TABLE_CHANTIER, Mode=Default}"
                             ItemTemplate="{StaticResource Mise_en_forme_liste}"
                             IsSynchronizedWithCurrentItem="true"
                             />
                    <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" >
                    	<GridSplitter.Background>
                    		<SolidColorBrush Color="#FFF6D4C4"/>
                    	</GridSplitter.Background>
                    </GridSplitter>
 
 
                    <Grid Grid.Column="2" Height="112.716" VerticalAlignment="Top" Margin="8.6,0,0,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="180" />
                            <ColumnDefinition Width="10" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="28" />
                            <RowDefinition Height="28" />
                            <RowDefinition Height="28" />
                        </Grid.RowDefinitions>
 
                        <Label Name="Label1" Margin="38.642,0,0,0">Equipe:</Label>
                        <TextBox    Name="txtselection1" Grid.Column="2" Grid.Row="0"                            
                                    VerticalAlignment="Center"
                                    Text="{Binding ElementName=ListBox1, Path=SelectedItem.NOM_EQUIPE, Mode=Twoway}"
                                    Style="{StaticResource Lecture_Seule}" />
 
                        <Label Grid.Row="1" Name="Label2" Margin="38.642,0,0,0">Nom du chantier:</Label>
                        <TextBox    Name="txtselection2" Grid.Column="2" Grid.Row="1"                            
                                    VerticalAlignment="Center"
                                    Text="{Binding ElementName=ListBox1, Path=SelectedItem.NOM_CHANTIER, Mode=Twoway}"
                                    Style="{StaticResource Lecture_Seule}"/>
 
                        <Label Grid.Row="2" Name="Label3" Margin="38.642,0,0,0">Date de demarrage:</Label>
                        <TextBox    Name="txtselection3" Grid.Column="2" Grid.Row="2"                            
                                    VerticalAlignment="Center"
                                    Text="{Binding ElementName=ListBox1, Path=SelectedItem.DATE_DEMARRAGE, Mode=Twoway}" 
                                    Style="{StaticResource Lecture_Seule}"/>
                    </Grid>    
 
                    <CheckBox x:Name="_CheckBox_Lecture_Seule" Grid.Column="2" Height="20.006" Margin="47.242,132,0,0" Content="Lecture seule" 
                              VerticalAlignment="Top" HorizontalAlignment="Left" Width="138.164"/>
 
                    <dg:DataGrid IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=TABLE_TOUPIE}"  Grid.Column="2" Margin="0,159.984,0,169.983" />
 
 
                </Grid>
 
            </TabItem>
 
        </TabControl>
    </Grid>
Voili, voilou,