views:

812

answers:

1

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.

A: 

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...

BFree
The code I've posted is all the databinding code there is. It works as elsewhere in the application. The user picks a table/view, and I display the databound rows. My understanding is that the DataTable has enough metadata in it to allow the DGV to automatically generate columns, so I don't have to.
Mike Hofer