我有一个包含返回数据表的方法的基窗体类:
protected DataTable GetTableData(string sql, OracleConnection connection)
{
DataTable table = null;
OracleDataAdapter adapter = null;
try
{
table = new DataTable();
adapter = new OracleDataAdapter(sql, connection);
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
adapter.Fill(table);
}
catch (Exception e)
{
MessageBox.Show("An error occurred while trying to process your request:\n\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (null != adapter)
{
adapter.Dispose();
}
}
return table;
}
另一个窗口是它的子类,并按如下方式调用它:
private void LoadViewData(OracleConnection connection)
{
DataTable table = null;
try
{
var sql = "SELECT * FROM " + this.ObjectName;
table = GetTableData(sql, connection);
this.resultBindingSource.DataSource = table;
}
catch (Exception e)
{
MessageBox.Show("An error occurred while trying to process your request:\n\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.sqlEditor.Focus();
}
}
resultBindingSource
是一个
System.Windows.Forms.BindingSource
. 设置为
DataSource
A的性质
System.Windows.Forms.DataGridView
. (表达,
this.ObjectName
,计算为数据库中表或视图的名称。)
当我在调试器中运行代码时,我可以看到SQL执行得很好。我可以看到
DataTable
包含数据。我可以看到
DataGridView
控件已正确绑定到数据源,并且我可以通过其
数据来源
属性。但是,控件本身不显示任何数据。没有行或列标题,也不显示任何数据。
我想尽一切办法查明这个问题的原因。此代码的工作方式与另一个表单上显示的完全相同。我尝试删除有问题的控件并重新创建它们,但没有效果。我查阅了有关msdn的文章,了解如何正确地将数据绑定到
数据表格控件
控制。我试过了没有
OracleCommandBuilder
(这对我来说似乎不必要,因为这是数据的只读视图)。
坦率地说,我没有主意。很可能是我忽略了一些相当明显的事情。我知道数据绑定是有效的,因为我以前做过,而且非常成功。
任何指向正确方向的指针都将受到极大的赞赏。