Click here to Skip to main content
15,925,440 members
Home / Discussions / C#
   

C#

 
GeneralRe: Multiple forms and datagridviews Pin
bwood202018-May-09 8:44
bwood202018-May-09 8:44 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute18-May-09 9:54
Henry Minute18-May-09 9:54 
GeneralRe: Multiple forms and datagridviews Pin
bwood202018-May-09 12:54
bwood202018-May-09 12:54 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute18-May-09 13:01
Henry Minute18-May-09 13:01 
GeneralRe: Multiple forms and datagridviews Pin
bwood202019-May-09 7:13
bwood202019-May-09 7:13 
GeneralRe: Multiple forms and datagridviews [modified] Pin
Henry Minute19-May-09 8:33
Henry Minute19-May-09 8:33 
GeneralRe: Multiple forms and datagridviews Pin
bwood202019-May-09 9:22
bwood202019-May-09 9:22 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute19-May-09 10:52
Henry Minute19-May-09 10:52 
The second Form method seems to be as good as any other, from what you have told me. So on that basis.

What happens when the user clicks the button, depends on whether there will only ever be one instance of Form2 where the data displayed will change depending on whatever, or whether there will be more than one instance, each one created specifically to display one set of data.

If there will only be one Form2, in Form1 add
private Form2 docsForm = null;
private Form2 DocsForm
{
    get
    {
        if (this.docsForm == null)
        {
            this.docsForm = new Form2();
        }

        return docsForm;
    }
}


then whenever Form1 needs to refer to the Form2 instance just use DocsForm (the property name) NOT docsForm (the field name). This property uses what is known as 'Lazy Initialization', you can look it up later. It just means that the thing (Form2 in this case) only gets created when it needs to be. The rest of the time the existing instance is used. I have made it private deliberately.

If there will be many instances of Form2 they will be created inside the click handler for the button, so the above will not be needed.

The button click handler, just for illustration purposes I have called it btnShowDocs
private void btnShowDocs_Click(object sender, EventArgs e)
{
                // ************************************
                // Only if there are to be many Form2s
                // ************************************
    Form2 DocsForm = new Form2();
                // ************************************

                DocsForm.DataKey = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
                DocsForm.Show();  // or .ShowDialog(), depends on whether need access to Form1 again before Form2 closes
}


There has to be some piece of data in Form1.dgv1, that will enable Form2 to decide the correct data to show, purely for illustration I have said that that data is the value of the first column in the selected row of dgv1.

But you will have to let me know what that piece of data is. It could be the value of a cell in dgv as above, or the whole row, each cell in the row determining the data for one of the dgvs of Form2. If the trigger data is not in dgv1, then what is it?

Then for Form2, something like:
public partial class Form2 : Form
{
    private bool dataDisplayed = false;

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        this.LoadData();
    }

    private void LoadData()
    {
        // here will go the code to load the data into the datagrids

        this.dataDisplayed = true;
    }

    private string dataKey = string.Empty;
    public string DataKey
    {
        get
        {
            return this.dataKey;
        }

        set
        {
            if (this.dataKey != value)
            {
                this.dataKey = value;
                if (this.dataDisplayed)
                {
                    this.LoadData();
                }
            }
        }
    }
}


So to help further I need to know what on Form1 enables Form2 to decide what to show. So that I can help you to decide how to pass it to Form2.

The way that you are currently doing it Form1 loads Form2 (via button-click) Form2 then goes to Form1 to get data to load, is going round in circles, never a good idea. Sooner or later it will jump up and bite you in a nasty place. Smile | :)

Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

GeneralRe: Multiple forms and datagridviews Pin
bwood202019-May-09 12:36
bwood202019-May-09 12:36 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute19-May-09 13:48
Henry Minute19-May-09 13:48 
GeneralRe: Multiple forms and datagridviews Pin
bwood202020-May-09 10:02
bwood202020-May-09 10:02 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute20-May-09 10:14
Henry Minute20-May-09 10:14 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute20-May-09 10:19
Henry Minute20-May-09 10:19 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 6:25
bwood202021-May-09 6:25 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute21-May-09 7:01
Henry Minute21-May-09 7:01 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 8:48
bwood202021-May-09 8:48 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute21-May-09 9:08
Henry Minute21-May-09 9:08 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 9:54
bwood202021-May-09 9:54 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute21-May-09 10:42
Henry Minute21-May-09 10:42 
GeneralRe: Multiple forms and datagridviews Pin
bwood202021-May-09 13:01
bwood202021-May-09 13:01 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute22-May-09 9:59
Henry Minute22-May-09 9:59 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute22-May-09 10:05
Henry Minute22-May-09 10:05 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute22-May-09 10:08
Henry Minute22-May-09 10:08 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute22-May-09 10:12
Henry Minute22-May-09 10:12 
GeneralRe: Multiple forms and datagridviews Pin
bwood202022-May-09 10:19
bwood202022-May-09 10:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.