Bonjour,
Je débute en c# et j'essaie de faire un tourner un code que j'ai trouvé sur le net.
Mon soucis est que je voudrais que la valeur de la premiere ligne du datagrid, se charge automatiquement au lancement de la form dans un textbox.
J'ai ce code qui fonctionne bien, mais il n'affiche aucune valeur dans le textbox, si je ne clique pas sur une ligne du datagrid.
Le code:
Comment je pourrais faire pour que la premiere ligne s'affiche dans le textbox comme si je cliquais dessus ?
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 namespace SQLiteTEST { public partial class Form1 : Form { private SQLiteConnection connection; private String SQLSelect = "SELECT * FROM User"; public Form1() { InitializeComponent(); connection = new SQLiteConnection("Data Source=BddTest.s3db;Version=3;"); } private void search() { dataGrid1.RowEnter -= dataGrid_RowEnter; if (connection.State != ConnectionState.Open) connection.Open(); SQLiteCommand command = connection.CreateCommand(); command.CommandText = SQLSelect; DataTable dt = new DataTable(); SQLiteDataAdapter da = new SQLiteDataAdapter(command); da.Fill(dt); dataGrid1.DataSource = dt; connection.Close(); dataGrid1.RowEnter += dataGrid_RowEnter; } private void dataGrid_RowEnter(object sender, DataGridViewCellEventArgs e) { int ID = int.Parse(dataGrid1.Rows[e.RowIndex].Cells[0].Value.ToString()); String Data1 = (String)dataGrid1.Rows[e.RowIndex].Cells[1].Value; txtId.Text = ID.ToString(); txtName.Text = Data1; } private void Form1_Load(object sender, EventArgs e) { search(); } private void dataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e) { MessageBox.Show("erreur"); } private void button1_Click(object sender, EventArgs e) { var form_programme = new Form2(); form_programme.Show(); this.Hide(); } private void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void txtName_TextChanged(object sender, EventArgs e) { } private void txtId_TextChanged(object sender, EventArgs e) { } private void dataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } }
Merci d'avance pour vos conseils ou exemple de code.
Partager