Click here to Skip to main content
16,005,038 members
Home / Discussions / C#
   

C#

 
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 
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 
I have modified my suggestions from the previous post in light of this information, using your member names where I noticed them and some of your code, although I have suggested moving it to MappingsForm rather than having it in Form1. The reason is that in OOP programming, as far as is possible, each object (and a Form is an object) should be responsible for handling its own data and controlling access to that data.

For Form1:
    private MappingsForm mappingsForm = null;
    private MappingsForm MappingsForm
    {
        get
        {
            if (this.mappingsForm == null)
            {
                this.mappingsForm = new MappingsForm();
            }

            return mappingsForm;
        }
    }

private void btnShowDocs_Click(object sender, EventArgs e)
{
        MappingsForm.DataKey = this.dataGridView1.Rows[0];
        MappingsForm.Show();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.mappingsForm != null)
{
    this.mappingsForm.Dispose();
    this.mappingsForm = null;
}
}



For MappingsForm:
public partial class MappingsForm : Form
{
    private bool dataDisplayed = false;

    public MappingsForm()
    {
        InitializeComponent();
    }

    private void MappingsForm_Load(object sender, EventArgs e)
    {
                if (this.dataKey != null)
                {
            this.LoadData();
                }
    }

    private void LoadData()
    {
        // this is part of your code, slightly modified
                for (int c = 0; c < this.dataKey.Columns.Count; c++)
                {
                    if(this.dataKey.Cells[c].Value.ToString() == "DocType")
                    {
                        //pass data to dgv1
                    }
                    else if (this.dataKey.Cells[c].Value.ToString() == "PurposeType") // else here to avoid unnecessary processing
                    {
                        //pass data to dgv2
                    }
                    else if (this.dataKey.Cells[c].Value.ToString() == "SomeOtherType") // else here to avoid unnecessary processing
                    {
                        // and so on
                    }

                    // This could also be written like this, so either have the ifs as above OR this
                    switch (this.dataKey.Cells[c].Value.ToString())
                    {
                        case "DocType":
                            //pass dataKey.Cells[c].Value to dgv1
                            break;
                        case "PurposeType":
                            //pass dataKey.Cells[c].Value to dgv2
                            break;
                        case "SomeOtherType":
                            // and so on
                            break;
                    }
                }
        this.dataDisplayed = true;
    }

    private DataGridViewRow dataKey = null;
    public DataGridViewRow DataKey
    {
        get
        {
            return this.dataKey;
        }

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


When the data in the dgvs on MappingsForm changes Form1 should be notified by it subscribing to events raised by MappingsForm.
NOT by MappingsForm calling methods on Form1 directly.

The reasons for my concerns are that the current methodology breaks the 'rules' of OOP programming, most notably 'Data Encapsulation' and 'Data Hiding' the explanations of these are too long to go into in a forum like this, but Google them for better explanations than I can give here.

Basically it's like building a Bank with 87 doors v building one with one door. The first is much harder to keep safe.

BTW I noticed that on your Form1 code the button click handler was button12_Click, months down the line you will look at the code and say to yourself, "Now what on earth did button12 do, oh I see (if your lucky), I thought that button8 did that!" Try wherever possible to use descriptive names for your controls, it makes things much easier for maintenance.

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
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 
GeneralRe: Multiple forms and datagridviews Pin
bwood202026-May-09 4:08
bwood202026-May-09 4:08 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute26-May-09 4:15
Henry Minute26-May-09 4:15 

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.