Data of a Datagridview configured in another Form

Asked

Viewed 964 times

0

I have the Form1 who owns a dataGridView and a button to add content to this Grid. By selecting this button, I call Form2 which has 2 Textbox and a Combobox to fill in the Name, Directory and Format (for example), plus a button OK to add the line to dataGridView in Form1.

When I get back on Form1, the dataGridView needs to be showing the inserted content. What happens is that my Grid has three columns: Name, Format and a button column to, when clicking on it, reload that screen of the Form2 with the previously filled information loaded again in each field to be able to change or only consult.

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

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 the Grid. So I thought about creating a class with a method name, path and format (get and set for each one) 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 to store in a Dataset. But then I thought, I urge the class on mine Form2 and pass the filled values in the fields, but then what I do in my Form1?

I think it’s all very confusing, I would appreciate it if someone could explain a clearer method of solving this situation and please be very specific because I’m a beginner.

1 answer

2


It doesn’t work if you create another instance of Form1 within the Form2. You would have to pass the instance that is open from Form1 or insert directly into it.

The simplest thing is:

By clicking OK, Form2 will return Dialogresult.OK and will have the properties TxtNome and TxtPath filled with the necessary 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 on the 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 that I help you with it too.

  • It worked like a charm! I was thinking, I’ll have more data on that editing screen, so instead of calling the query screen with a button, I could call with a double click. Do you have any idea about this?

  • did the functions, and updated the answer. see if it helps you and do not forget to mark as reply

  • I don’t know what’s going on because it picks the right values at the time I edit, but then appears wrong on the grid.

  • where has Cells[0] and Cells[1] you can change the index of these cells or name the columns. Ex: Cells[ColumnPath.Name]

  • Sorry, lack of attention! I put two repeated as I added more! Thank you so much for the help, even!!!

  • hassle-free =]

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.