Bonjour à tous,
Je m'exerce un peu en C# en ce moment et je sèche un peu devant un problème.
J'ai créer une table avec id,nom,prenom.
Ma combobox se remplit donc automatiquement avec les id de ma table et après sélection, je clic sur un bouton qui m'affiche une fenêtre où il est noté : "L'ID x correspond à l'utilisateur y" où y correspond au nom ou prenom.
J'aimerais pouvoir récupérer le prenom et le nom en même temps dans la même fenêtre.
Mon xaml :
Le 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 <Window x:Class="testLiaisonDB.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel Orientation="Horizontal" Width="390" Height="24"> <Label Content="Select ID :" Height="22" HorizontalAlignment="Right"></Label> <ComboBox Name="select_id" Width="120" Height="22" ItemsSource="{Binding}"></ComboBox> <StackPanel> <Button Margin="26,0" Name="button_id" Content="Go!" Width="70" Height="20" Click="btnId_click"></Button> </StackPanel> </StackPanel> </Grid> </Window>
J'ai essayé de faire un deuxième lot de SqlDataAdapter et DataSet qui serait chargé de récupérer le prenom et le stocker dans une variable à afficher dans ma fenêtre mais je n'ai pas réussi.
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 namespace testLiaisonDB { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); BindComboBox(select_id); } public void BindComboBox(ComboBox ComboBoxName) { SqlConnection conn = new SqlConnection("maStringDeConnection"); SqlDataAdapter da = new SqlDataAdapter("SELECT id, nom, prenom FROM testTableAntonin", conn); DataSet ds = new DataSet(); da.Fill(ds,"testTableAntonin"); ComboBoxName.ItemsSource = ds.Tables[0].DefaultView; ComboBoxName.DisplayMemberPath = ds.Tables[0].Columns["id"].ToString(); ComboBoxName.SelectedValuePath = ds.Tables[0].Columns["nom"].ToString(); } private void btnId_click(object sender, RoutedEventArgs e) { MessageBox.Show("L'ID = " + select_id.Text + " correspond à l'utilisateur = " + select_id.SelectedValue.ToString()); } } }
Merci de votre aide
Partager