I am trying to make a DataGridView control get populated from a DataTable, that itself gets filled with the results of a sql query on a Oracle database. This should happen after double-clicking the table name from a ListBox control.
This works fine the first time I double-click a table name, but the second time it will only show the column names and no rows.
What I have discovered is that sorting the DataGridView based on any column will cause the rows to suddenly appear, which makes me suspect some kind of graphical/rendering glitch.
Is this expected behavior?
// event handler for ListBox
private void tables_MouseDoubleClick(object sender, MouseEventArgs e)
{
// tables is a ListBox populated with table names
int index = this.tables.IndexFromPoint(e.Location);
String table_name;
if (index != System.Windows.Forms.ListBox.NoMatches)
{
// reusing the DataTable
dt.Reset();
table_name =tables.Items[index].ToString();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
// case sensitive
cmd.CommandText = String.Format(@"select * from ""{0}""",table_name);
cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(dt);
dt.TableName = table_name;
label7.Text = table_name;
// these three lines don't actually accomplish anything in this case
table.DataSource = null;
table.Rows.Clear();
table.DataSource = dt;
// uncommenting these lines produces the expected behavior
//DataGridViewColumn c = table.Columns[0];
//table.Sort(c,System.ComponentModel.ListSortDirection.Descending);
}
}
After second double-click(without sorting)
After second double-click(with sorting)
Looks like the problem is caused by the DataTable.Reset call.
Although it's not your code problem (most likely a bug in DataTable or DataView), I would suggest using Clear method instead of Reset or creating a new DataTable instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With