I'm trying to put 3 binded DataGridViewComboBoxCell in my datagridview,
one for 'Categorie' table, the second for 'Article' table and the third for
'ArticleNonCon' table. when i select a categorie the second
DataGridViewComboBoxCell should give me the articles of this categorie and
when i select an article the third DataGridViewComboBoxCell should give me
the serial numbers of this article. This is the code i tried:
in the button click that shows the form:
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand catCmd = new SqlCommand("SELECT IdCategorie, LibCategorieFr FROM Categorie", con);
DataTable catDt = new DataTable();
catDt.Load(catCmd.ExecuteReader());
((DataGridViewComboBoxColumn)dataGridView1.Columns["CategorieColumn"]).DataSource = catDt;
((DataGridViewComboBoxColumn)dataGridView1.Columns["CategorieColumn"]).DisplayMember = "LibCategorieFr";
((DataGridViewComboBoxColumn)dataGridView1.Columns["CategorieColumn"]).ValueMember = "IdCategorie";
and in CellEndEdit event of the datagridview:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
switch (dataGridView1.Columns[e.ColumnIndex].Name)
{
case "CategorieColumn":
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand artCmd = new SqlCommand("SELECT * FROM Article WHERE IdCategorie = @IdCategorie", con);
artCmd.Parameters.AddWithValue("@IdCategorie", dataGridView1.CurrentRow.Cells["CategorieColumn"].Value);
DataTable articleDt = new DataTable();
articleDt.Load(artCmd.ExecuteReader());
((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).DataSource = articleDt;
((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).DisplayMember = "LibArticleFr";
((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).ValueMember = "CodeArticle";
con.Close();
break;
case "Column3":
if (con.State != ConnectionState.Open)
{
con.Open();
}
SqlCommand numSerieCmd = new SqlCommand("SELECT * FROM ArticleNonCon WHERE CodeArticle = @CodeArticle", con);
numSerieCmd.Parameters.AddWithValue("@CodeArticle", dataGridView1.CurrentRow.Cells["Column3"].Value);
DataTable numSerieDt = new DataTable();
numSerieDt.Load(numSerieCmd.ExecuteReader());
((DataGridViewComboBoxColumn)dataGridView1.Columns["NumSerieColumn"]).DataSource = numSerieDt;
((DataGridViewComboBoxColumn)dataGridView1.Columns["NumSerieColumn"]).DisplayMember = "NumSerieArticle";
((DataGridViewComboBoxColumn)dataGridView1.Columns["NumSerieColumn"]).ValueMember = "id";
con.Close();
break;
}
}
This code works perfectly for the first run, but whent i try to change the article and select the serial numbers of this article from the third datagridviewComboboxCell i got this exception:
System.ArgumentException:DataGridViewComboBoxCell value is not valid
This exception occured also when i try to close the application.
So how to fix this error or in which datagridview event i should put my code?
Thanks in advance.