This is a migrated thread and some comments may be shown as answers.

Add row from another form

4 Answers 171 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Guy
Top achievements
Rank 1
Guy asked on 29 Oct 2010, 03:21 PM
Hi,

I'm sure this is an easy question but for some reason I cannot get my head round it.

I have a form (Form1) with several RadGridViews on it. I have created a second form (Form2) that the user completes to add rows to each of the gridviews.

I'm stuck on being able to access the gridviews from the second form to complete the radgridview.rows.add.

I have created an object for Form1 inside Form2 but that's where I get stuck.

Can someone please point me in the right direction?

Guy

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Oct 2010, 10:46 PM
Hello Guy,

There are a few solutions on how to do this, the easiest would be to create properties for the grids inside Form1, and when you create Form2 just create another constructor that takes Form1 as a parameter and just pass the current Form1's instance to Form2 on creation.
Store that instance in a variable, and that way you will have access to all the grids using those properties.

This is the easiest way, other ways, (better, cleaner) could include:
- Events that Form2 could fire when the data has been filled in by the user and confirmed, and Form1 would register to these events and take the data from either custom event args, or again the form itself.
- if you are using ShowDialog() you can also use a custom dialog result and pass the data back to Form1 trough that.

I hope that based on these suggestions you will be able to find a solution that suits your design.

If you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Guy
Top achievements
Rank 1
answered on 31 Oct 2010, 04:25 PM
Hi Emanuel,

Thank you for your reply.

Could you possibly expand on your first solution please as I am very new to C#

Kind Regards,

Guy
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 01 Nov 2010, 12:22 PM
Hello again,

Sorry for getting back to you so late, but you can try something like this:
Here, Form1 is the parent form and let's say it holds just one grid: radGridView1, in order to be able to access this from other forms you can just create a property for it (with just a setter) set's say called Grid1.
After this, when you create the child form you just pass the Form1 instance as a parameter and set it as the parentForm for Form2.
After this, you can just say parentForm.Grid1 .... and perform all the operations required like you would normally do on the grid from the main form.
Please take a look at the following example:
public partial class Form1 : Form
{
    private RadGridView radGridView1;
 
    public RadGridView Grid1
    {
        get { return radGridView1; }
    }
 
    public Form1()
    {
        InitializeComponent();
    }
 
    private void ShowEditForm()
    {
        var editForm = new Form2(this);
        editForm.Show();
    }
 
}
 
    public class Form2 : Form
    {
        private Form1 parentForm;
 
        public Form2(Form1 parentForm)
        {
            this.parentForm = parentForm;
        }
 
        public void SetNewData()
        {
            var grid1 = parentForm.Grid1;
            // handle operations here
        }
    }
Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Guy
Top achievements
Rank 1
answered on 01 Nov 2010, 01:28 PM
Hi,

That's excellent! Thank you so much.

Best regards,

Guy
Tags
GridView
Asked by
Guy
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Guy
Top achievements
Rank 1
Share this question
or