代码之家  ›  专栏  ›  技术社区  ›  Mike Hofer

尽管数据源已满,但数据绑定DataGridView为空

  •  0
  • Mike Hofer  · 技术社区  · 16 年前

    我有一个包含返回数据表的方法的基窗体类:

    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 (这对我来说似乎不必要,因为这是数据的只读视图)。

    坦率地说,我没有主意。很可能是我忽略了一些相当明显的事情。我知道数据绑定是有效的,因为我以前做过,而且非常成功。

    任何指向正确方向的指针都将受到极大的赞赏。

    1 回复  |  直到 16 年前
        1
  •  1
  •   BFree    16 年前

    我试着用你在这里提到的片段重新创建你的程序。实际上,我并没有从数据表中获取数据,但这无关紧要。我是这样做的:

    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;
            }
        }
    

    这和你的差不多吗?我一点问题也没有。根据你发布的内容,这应该有效。你确定你把每件事都正确地绑在一起了吗?如果可能,发布更多绑定代码…

    推荐文章