Add row from another form

5 posts, 1 answers
  1. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 29 Oct 2010 Link to this post

    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
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Oct 2010 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 31 Oct 2010 Link to this post

    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
  5. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 01 Nov 2010 Link to this post

    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
  6. Guy
    Guy avatar
    60 posts
    Member since:
    Sep 2010

    Posted 01 Nov 2010 Link to this post

    Hi,

    That's excellent! Thank you so much.

    Best regards,

    Guy
Back to Top
UI for WinForms is Visual Studio 2017 Ready