Changing rows in DataGridView / Opening a form with different design

2

I have a problem with a school project, as I said in the title, I need to change rows and open a form that already has design , but with different things, come on:

The program is very simple, a datagrid , with Name, Id, Phone and a checkbox that defines whether the person is active or not. Underneath it there are 4 buttons, Insert , Change , Delete and Close . The Insert , opens another form with textboxes to fill the data, and 2 (Add / Close) buttons, and Delete have already been made.

On the Change button, I have to open the same form of the Insert button , but the Add button has to be renamed, changed, and instead of adding a new row , this will only change the values of it. I tried to look for ways to do this, but so far nothing.

If you can help me, I thank you very much, if you do not understand, I am willing to give you the instructions of my project so that you understand better. Thanks!

Note: This has nothing to do with SQL, several of the results that I looked for in google in YT had to do with SQL.

    
asked by anonymous 04.09.2016 / 00:38

2 answers

2

Another alternative is to use a control variable to indicate what behavior the button will have.

On the secondary form, create the variable modo :

namespace WindowsFormsApplication1
{
    public partial class FormCadastro : Form
    {
        public string modo = "Inserir";

        // ...

In event Show of the secondary form do:

private void formularioCadastro_Shown(object sender, EventArgs e)
{
    // Altere "botaoAdicionar" para o nome do botão que você quer 
    this.botaoAdicionar.Text = modo; // Inserir ou Alterar
}

In the Click event of the button you want to change the behavior do:

private void botaoAdicionar_Click(object sender, EventArgs e)
{
    switch (modo)
    {
        case "Inserir":
            // Códigos para "inserir" aqui...
            break;
        case "Alterar":
            // Códigos para "alterar" aqui...
            break;
        default:
            break;
    }
}

In the event Click of the Insert button of the main form do:

private void botaoInserir_Click(object sender, EventArgs e)
{
    using (FormCadastro formCadastro = new FormCadastro())
    {
        formCadastro.modo = "Inserir";
        formCadastro.ShowDialog();
    }
}

Do the same on the Change button, but change the mode:

private void botaoAlterar_Click(object sender, EventArgs e)
{
    using (FormCadastro formCadastro = new FormCadastro())
    {
        formCadastro.modo = "Alterar";
        formCadastro.ShowDialog();
    }
}
    
04.09.2016 / 01:36
2

For this you will do so: Use checkbox to set which row you want to change; Create an instance of the form you are using to create a new record:

var f = new MeuFormCadastro();

Then you will use this variable f to populate the textbox of the form:

f.textBoxNome.Text = row.Nome;
f.textBoxTelefone.Text = row.telefone;

Then you will finally see this form on the screen;

    
04.09.2016 / 00:58