开发者

Updating the gridview in one form

开发者 https://www.devze.com 2023-03-29 16:57 出处:网络
I have a small issue with DataGridView. I have two forms, in one form the data will be populated by the DataGridView.

I have a small issue with DataGridView.

I have two forms, in one form the data will be populated by the DataGridView.

When we click on the column header开发者_如何学运维 of the gridview the other form will be opened with the corresponding column values.

I have done some manipulations on the data in form2 so before closing form2 I want to update the form1 gridview with these details...

I have seen some details like eventhandler attachment in form1, but I didn't find an exact answer.


Form 2 code

    public event EventHandler<UpdatedEventArgs> updateEvent;

    public class UpdatedEventArgs : EventArgs
    {
        public string SomeVal { get; set; } // create custom event arg for your need
    }

    protected virtual void OnFirstUpdateEvent(UpdatedEventArgs e)
    {
        if (updateEvent != null)
            updateEvent(this, e);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        UpdatedEventArgs eventData = new UpdatedEventArgs(); 
        eventData.SomeVal = "test"; // set update event arguments, according to your need

        OnFirstUpdateEvent(eventData);
    }

    public Form2()
    {
        InitializeComponent();
    }

Form 1 code

    public Form1()
    {
        InitializeComponent();

        Form2 form2 = new Form2();
        form2.updateEvent += new EventHandler<Form2.UpdatedEventArgs>(form2_updateEvent); // create event handler to update form 1 from form 2
        form2.Show();
    }

    void form2_updateEvent(object sender, Form2.UpdatedEventArgs e)
    {
        if (e != null && e.SomeVal != null)
        {
            // Do the update on Form 1 
            // depend on your event arguments update the grid  
            //MessageBox.Show(e.SomeVal); 
        }

    }


By working with DataGrid you most likely use DataTable like a model. I would say pass like a parameter that DataTable to the form2 and manipulate THE SAME DataTable object in form2, so the changes made on its rows and commited will automatically reflect on form1.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号