DataGridView Selection Changed
bonsoir
Je développe une application gestion BD.j'ai un combobox et une gridview qui affiche des info.
voici une part du code de l'evenement selectedindexchange:
Code:
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
| private void Combo_Prov_SelectedIndexChanged(object sender, EventArgs e) // remplir gridview
{
nomprov_to_codprov(Combo_Prov.Text);
//try
// {
if (accConnection.State == ConnectionState.Open)
{
StrSQL = "SELECT Contact.Code_contact AS Code ,Contact.Fonction, Contact.FirstName AS Nom, Contact.LastName AS Prénom, Contact.Phone_contact1 AS Téléphone1, Contact.Phone_Contact2 AS Téléphone2 FROM Province INNER JOIN Contact ON Province.Code_Prov = Contact.Code_Prov WHERE (((Province.Name_Prov)=@param2)) ";
strSQL2 = "SELECT Province.Phone_Prov, Province.Phone_office FROM Province WHERE (((Province.Name_Prov)=@Param2))";
accCommand.Connection = accConnection;
accCommand.CommandText = StrSQL;
accCommand.CommandType = CommandType.Text;
accCommand.Parameters.Add("@Param2", OleDbType.Char).Value = Combo_Prov.Text;
ContactAdapter = new OleDbDataAdapter(accCommand);
ContactTable = new DataTable();
ContactAdapter.Fill(ContactTable);
BindingSource bS = new BindingSource();
contactDataGridView.DataSource = ContactTable;
bS.DataSource = ContactTable;
contactDataGridView.Columns[0].Visible = false; |
et voici l'evenement contactDataGridView_SelectionChanged
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private void contactDataGridView_SelectionChanged(object sender, EventArgs e)
{
DataGridViewRow ligne = contactDataGridView.CurrentRow;
MessageBox.Show(contactDataGridView.RowCount.ToString());
// index du row
MessageBox.Show(ligne.Cells[1].Value.ToString());
index_row = contactDataGridView.CurrentRow.Index;
//identifiant de l'enregistrement
string indentifiant = (ligne.Cells[0].Value.ToString());
identifiant = int.Parse(indentifiant);
fonction = (ligne.Cells[1].Value.ToString());
nom = (ligne.Cells[2].Value.ToString());
prenom = (ligne.Cells[3].Value.ToString());
Telephone1 = (ligne.Cells[4].Value.ToString());
Telephone2 = (ligne.Cells[5].Value.ToString());
} |
Lors de la première exécution et changement de la valeur du combo tout se passe bien mais une fois je change une autre fois la valeur du combo y a une exception de type object not set to a reference
J ai utilisé des breakpoints pour voir de plus pres,alors j ai constaté que la variable ligne à la première exécution est remplie mais une fois je passe à une autre valeur du combo elle est NULL et c est ce qui déclenche l'erreur
Merci pour votre aide