NHibernate, WinForms, and DataBinding - do they play well together?

I use WinForms data binding to display data from a database mapped to Fluent NHibernate, and this works great.

For example, I can just set the DataSource DataGridView property from the IList object property, and voila - there is all the data!

But now I need to start adding and saving new rows of data, and this is not so good. I thought I could just turn on the AllowUserToAddRows property of the grid, and new lines would be added to the underlying IList in essence, but that didn't work.

Then, after a little search, I tried to set the DataSource property to a BindingList that was populated from IList, but this is not updated with new lines.

During my searches, I also met several people who reported difficulties with WinForms and DataBinding in general, which makes me wonder if I will continue this approach.

Is the DataBinding approach appropriate? If so, can anyone suggest where I am wrong?

Or is it best to handle all the DataGridView events associated with adding a new row and writing custom code to add new objects to the IList property in my object?

Other offers? (although I don't think switching to WPF would be an option, no matter how much better data binding could be)

+3
source share
1 answer

( ) nHibernate- ? , , DataGridView, .

:

  • , .
  • . .
  • BindingSource, .
  • BindingSOurce AllowNew true. List , DataGridVieww " ", AllowUsersToAddRows = true.

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

, "".

+5

Source: https://habr.com/ru/post/1728995/


All Articles