Data from a DataGridView configured on another Form

0

I have Form1 that has a dataGridView and a button to add content to this Grid. When I select this button, I call the Form2 that has 2 TextBox and a ComboBox to fill in the Name, Directory, and Format (for example) in addition to a OK button to add the line to the dataGridView in Form1 .

When I get back on Form1 , the dataGridView must be showing the content entered. What happens is that my Grid has three columns: Name, Format and a button column to click on it to reload that screen of Form2 with the previously filled information loaded in each field again to be able to change or just check it out.

First I tried to create a function to add the lines to my Grid by parameters, as the code below shows:

Form 1

public Form1()
{
    InitializeComponent();
}

public void AddRows(string nome, string path)
{
    dataGridView1.Rows.Add(nome, path);
    dataGridView1.Update();
}

Form 2

public Form2()
{
    InitializeComponent();
}

private void bt1_Click(object sender, EventArgs e)
{
    var nome = txt_nome.Text;
    var path = txt_path.Text;

    var form1 = new Form1();
    form1.AddRows(nome, path);
}

However, it ended up not working, showing nothing on Grid. So I thought of creating a class with a name, path, and format ( get and set ) method to store everything in a list of objects (I think that's it forgive me if I'm wrong because I'm a beginner). Or save to a DataSet . But then I thought, I instantiate the class in my Form2 and pass the values filled in the fields, but there, what do I do in my Form1 ?

I think it's all very confusing, I'd be grateful if anyone could explain a clearer method of resolving this and please be specific as I'm a beginner.

    
asked by anonymous 04.07.2017 / 02:33

1 answer

2

Does not work if you create another instance of Form1 within Form2 . You would have to pass the instance that is open from Form1 or do the insertion directly into it.

The simplest is:

  

When you click OK, Form2 will return DialogResult.OK and will have the properties TxtNome and TxtPath filled in with the required values.

public string TxtNome {get;set;}
public string TxtPath {get;set;}

public Form2()
{
    InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
   txt_nome.Text = this.TxtNome;
   txt_path.Text = this.TxtPath;
}

private void bt1_Click(object sender, EventArgs e)
{
    this.TxtNome = txt_nome.Text;
    this.TxtPath = txt_path.Text;
    this.DialogResult = DialogResult.Ok;    
}
  

Now no Form1 :

public Form1()
{
    InitializeComponent();
}


private void ButtonAdd_Click(object sender, EventArgs e)
{
      Form2 form = new Form2();
      if (form.ShowDialog() == DialogResult.OK)
      {
          AddRows(form.TxtNome, form.TxtPath);
      }
      //Se não retornar OK (um botão cancelar por exemplo) não faz nada

}    


private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >=0 && !dataGridView2.Rows[e.RowIndex].IsNewRow)
    {
        Form2 form = new Form2();

        form.TxtNome = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
        form.TxtPath = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();

        if (form.ShowDialog() == DialogResult.OK)
        {
            EditRow(e.RowIndex, form.TxtNome, form.TxtPath);
        }
    }
}
public void AddRows(string nome, string path)
{
    dataGridView2.Rows.Add(nome, path);
    dataGridView2.Update();
}
public void EditRow(int idx, string nome, string path)
{
    dataGridView2.Rows[idx].Cells[0].Value = nome;
    dataGridView2.Rows[idx].Cells[1].Value = path;
}

If you want the editing part of the items, put your button code inside the datagridview so I'll help you with it too.

    
04.07.2017 / 04:06