Changing Rows in Datagridview / Opening a form with different design

Asked

Viewed 520 times

2

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

The program is very simple, a datagrid, with Name, Id, Telephone and a checkbox that defines whether the person is active or not. Below it there are 4 buttons, Insert, Alter, Delete and Close. The of Insert, which opens another form with textboxes for data filling, and 2 buttons (Add/Close), and Delete have already been made.

On the button Alter, I have to open the same form as the Insert button, however the Add button has to be under another name, change, and instead of adding a new one row, this will only alter her values. I tried to find ways to do it, but so far nothing.

If you can help me I really appreciate it, if you don’t understand, I’m willing to give you the instructions for my project so that you understand better. Thank you!

OBS.: This has nothing to do with SQL, several of the results I searched on google on YT had to do with SQL.

  • Right, and what problem are you facing?

  • I have no idea how to do that. Sorry, I forgot to put that in the question...

2 answers

2

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

var f = new MeuFormCadastro();

Then you will use this variable f to fill in the textbox form:

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

Then at last you will display this form onscreen;

2


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

In the secondary form create the variable modo:

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

        // ...

At the event Show of the secondary form make:

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
}

At the event Click from 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;
    }
}

At the event Click button Insert from 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 button Alter, but change the way:

private void botaoAlterar_Click(object sender, EventArgs e)
{
    using (FormCadastro formCadastro = new FormCadastro())
    {
        formCadastro.modo = "Alterar";
        formCadastro.ShowDialog();
    }
}
  • I’m not getting the button to stay in "Change", first he was saying he needed a constructor that carried 0 arguments, so I made one, only that even clicking, or inserting, or changing, the insert button of the secondary form is only in insert.

  • 1

    Yeah, I know, I got it yesterday, but I couldn’t edit it, but thanks!

Browser other questions tagged

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