【问题标题】:Databound DataGridView Empty Despite Full DataSource尽管数据源完整,但数据绑定 DataGridView 为空
【发布时间】:2009-03-20 17:34:56
【问题描述】:

我有一个基础表单类,其中包含一个返回 DataTable 的方法:

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。它被设置为System.Windows.Forms.DataGridViewDataSource 属性。 (表达式this.ObjectName 的计算结果为数据库中表或视图的名称。)

当我在调试器中运行代码时,我可以看到 SQL 执行得很好。我可以看到 DataTable 包含数据。我可以看到DataGridView 控件正确绑定到数据源,并且我可以通过它的DataSource 属性看到数据表中的数据。但是,控件本身中不显示任何数据。没有行或列标题,也没有任何数据显示。

我已尽我所能确定此问题的原因。此代码的工作方式与另一个表单上显示的完全相同。我尝试删除有问题的控件并重新创建它们,但无济于事。我查阅了 MSDN 上有关如何正确地将数据绑定到 DataGridView 控件的文章。我尝试了使用和不使用OracleCommandBuilder(这对我来说似乎没有必要,因为这是数据的只读视图)。

坦率地说,我没有想法。我可能忽略了一些相当明显的事情。我知道数据绑定很有效,因为我之前已经成功地做到了。

任何正确方向的指针将不胜感激。

【问题讨论】:

    标签: c# .net data-binding datagridview


    【解决方案1】:

    我尝试使用您在此处提到的部分重新创建您的程序。我实际上并没有从数据表中获取数据,但这无关紧要。这是我所做的:

    public partial class Form1 : BaseForm
        {
            BindingSource source = new BindingSource();
            public Form1()
            {
                InitializeComponent();
                this.dataGridView1.DataSource = source;
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DataTable table = GetDataTable();
                this.source.DataSource = table;
            }
        }
    
        public class BaseForm : Form
        {
            protected DataTable GetDataTable()
            {
                DataTable result = new DataTable();
                result.Columns.Add("Name");
                result.Columns.Add("Age", typeof(int));
                result.Rows.Add("Alex", 27);
                return result;
            }
        }
    

    这与您的情况大致相同吗?我一点问题都没有。根据您发布的内容,这应该有效。您确定将所有内容正确绑定吗?如果可能,发布更多您的绑定代码...

    【讨论】:

    • 我发布的代码是所有的数据绑定代码。它与应用程序中的其他地方一样工作。用户选择一个表/视图,我显示数据绑定行。我的理解是DataTable里面有足够的元数据可以让DGV自动生成列,所以我不用。
    猜你喜欢
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2020-11-20
    • 1970-01-01
    • 2020-07-11
    • 2014-09-15
    相关资源
    最近更新 更多