R – NHibernate, WinForms, and DataBinding – do they play well together

data-bindingfluent-nhibernatenhibernatewinforms

I've been using WinForms databinding to display data from a database mapped with Fluent NHibernate, and that's been working great.

For example, I can just set a DataGridView's DataSource property from an entity's IList property, and voila – there's all the data!

But now I need to start adding and saving new data rows, and that's not going so well. I thought I'd be able to just enable the grid's AllowUserToAddRows property, and new rows would get added to the underlying IList in the entity, but that didn't work.

Then, after a little searching, I tried setting the DataSource property to a BindingList that was populated from the IList, but that's not being updated with new rows either.

During the course of my searches, I also came upon a few people reporting difficulty with WinForms and DataBinding in general, which makes me wonder if I should pursue that approach any further.

Is the DataBinding approach worth continuing? If so, can anyone suggest where I'm going wrong?

Or is it better to just handle all the DataGridView events associated with adding a new row, and writing my own code to add new objects to the IList property in my entity?

Other suggestions? (though I don't think switching to WPF is going to be an option, no matter how much better the databinding may be)

Best Answer

Can you load (or copy) your nHibernate entities into a generic List? If so, I have had good success in with two-way binding using a DataGridView bound to a generic List.

The key points are:

  • The generic list contains list objects where each is an instance of your custom class.
  • Your custom class must implement public properties for each of the fields to bind. Public fields didn't work for me.
  • Use a BindingSource to wrap the actual generic list.
  • The BindingSOurce allows you to set the AllowNew property to true. Binding directly to the List almost works, but the DataGridVieww does not display the "New row" line, even if AllowUsersToAddRows = true.

For example, add this code to a Form with a dataGridView1:

    private List<MyObject> m_data = new List<MyObject>();

    private BindingSource m_bs =new BindingSource();


    private void Form1_Load(object sender, EventArgs e)
    {

        m_data.Add(new MyObject(0,"One",DateTime.Now));
        m_data.Add(new MyObject(1, "Two", DateTime.Now));
        m_data.Add(new MyObject(2, "Three", DateTime.Now));

        m_bs.DataSource = m_data;
        m_bs.AllowNew = true;

        dataGridView1.DataSource = m_bs;
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.AllowUserToAddRows = true;

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        for (int i = 0; i < m_data.Count ; i++)
        {
            Console.WriteLine(string.Format("{0} {1}  {2}", m_data[i].ID, m_data[i].Name, m_data[i].DOB));                
        }
    }
}

public class MyObject
{
    // Default ctor, required for adding new rows in DataGridView
    public MyObject()
    {
    }

    public MyObject(int id, string name, DateTime dob)
    {
        ID = id;
        Name = name;
        DOB = dob;

    }

    private int m_id;
    public int ID
    {
        get
        {
            return m_id;
        }
        set
        {
            m_id = value;
        }
    }


    private string m_name;

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            m_name = value;
        }
    }

    private DateTime m_dob;

    public DateTime DOB
    {
        get
        {
            return m_dob;
        }
        set
        {
            m_dob = value;
        }
    }
}

When the form closes, the contents of the bound List are printed to the Output window.

Related Topic