0

I want to display in a datagridview informations of books that he's author is selected in combobox, in offline mode.

I wrote this code to fill the combobox with ahutor's name

private void Liste_des_Livres_Load(object sender, EventArgs e)
{
           DA = new SqlDataAdapter("SELECT * FROM Livre", con);
           DA.Fill(DS, "Liv");
           for (int i = 0; i < DS.Tables["Liv"].Rows.Count; i++)
           {
               comboBox1.Items.Add(DS.Tables["Liv"].Rows[i][2]);
           }
}

and this to display the informations and there's the problem

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DA = new SqlDataAdapter("SELECT * FROM Livre WHERE Auteur='" + comboBox1.Text + "';", con);
            DA.Fill(DS, "liv");
            dataGridView1.DataSource = DS.Tables["liv"];
        }

in the results when I click in one item of the combobox I got all informations of all authors + again the informarions of selected author.

I tried to add dataGridView1.Rows.Clear(); but it didn't work I got an exception System.ArgumentException : 'Impossible d'effacer cette liste.'

Sella
  • 51
  • 5
  • When you add rows in datagridview with datasource then can't use the clear() method. Instead try to clear datasource. You may set DataSource to null – ggeorge Dec 10 '20 at 17:11
  • The posted code does not show the code `dataGridView1.Rows.Clear();`… If the grid is data bound as it appears it is, then you can not (through code) remove the rows from the “GRID.” Since you are changing the grids `DataSource` with `dataGridView1.DataSource = DS.Tables["liv"];` … then there should be no reason to remove the existing rows as setting a new data source will remove the existing rows automatically. I am guessing this may be what you are asking. – JohnG Dec 10 '20 at 17:17
  • When you are offline where do you expect the data to come from? – jdweng Dec 10 '20 at 18:25

0 Answers0