Joaquim  Correia

Joaquim Correia

  • NA
  • 9
  • 23.3k

DataGridView Behaviour

Nov 14 2007 12:05 PM
Helo,

I got this problem with a c# DataGridView in VS2005:

The DataGridView has 2 columns: colArtigoGenerico, colArtigoFinal; and this columns are populated with data from 2 columns from a DataTable: "Detalhes".

1st i populate the DataGrid with a Datatable using this code:

string select = "SELECT a.CArtigoGenerico, a.CArtigoFinal " +
"FROM ArtigosGenericos a ORDER BY a.CArtigoGenerico ";
SqlDataAdapter adaptador = new SqlDataAdapter(select, ligacao);
adaptador.Fill(Detalhe, "Detalhes");

// Populate the Grid:
DGV.AutoGenerateColumns = false;
DGV.DataSource = Detalhe.Tables["Detalhes"];

DGV.Columns["colArtigoGenerico"].DataPropertyName = "CArtigoGenerico";
DGV.Columns["colArtigoFinal"].DataPropertyName = "CArtigoFinal";

ColArtigoGenerico.ValueMember = DGV.Columns[0].DataPropertyName;
ColArtigoFinal.ValueMember = DGV.Columns[1].DataPropertyName;


The above code is in a button_click event. And at the 1st time i run it, the grid is populated correctly. Then i set the DataSource to null and the DataGridView, as expected, collapses and shows no record.

Then i click again in the 1st button, and the DataGridView, this time, shows no record.

In a debug session i see that the DataTable has some records, and after the code line:
DGV.DataSource = Detalhe.Tables["Detalhes"];
The DGV.RowCount property returns 0, that's why it shows no record in the Grid.

But the DataTable that is the DataSource for the Grid has 3 records.
Why this behaviour in the 2nd and following times?

TIA,
Joaquim

Answers (2)