简体   繁体   中英

Databound DataGridView Empty Despite Full DataSource

I have a base form class that contains a method that returns a 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;
}

Another window is a subclass of it, and invokes it as follows:

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 is a System.Windows.Forms.BindingSource . It is set as the DataSource property of a System.Windows.Forms.DataGridView . (The expression, this.ObjectName , evaluates to the name of a table or view in the database.)

When I run through the code in the debugger, I can see that the SQL executes just fine. I can see that the DataTable contains data. I can see that the DataGridView control is properly bound to the data source, and that I can see the data in the data table through its DataSource property. However, no data is displayed in the control itself. There are no row or column headers, and no data is displayed whatsoever.

I have tried everything I can think of to pin down the cause of this problem. This code works exactly as shown on another form. I tried deleting the controls in question and recreating them, to no avail. I consulted the articles on MSDN on how to properly databind to a DataGridView control. I tried it with and without an OracleCommandBuilder (which doesn't seem necessary to me, since this is a read-only view of the data).

I'm frankly out of ideas. It's likely something fairly obvious that I've overlooked. I know that databinding works, because I've done it before with great success.

Any pointers in the right direction would be greatly appreciated.

I tried recreating your program using the pieces you mentioned here. I didn't actually get data from a datatable but that's irrelevant. Here's what I did:

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

Is this roughly the same thing you have? I had no issues at all. Based on what you're posting this SHOULD work. Are you sure you're binding everything to each other correctly? Post more of your binding code if possible...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM