Exemple #1
0
        private void frmAluno_Load(object sender, EventArgs e)

        {
            try

            {
                Aluno Alu = new Aluno();

                dsAluno.Tables.Add(Alu.Listar());

                bnAluno.DataSource = dsAluno.Tables["TBALUNO"];

                dgvAluno.DataSource = bnAluno;

                bnvAluno.BindingSource = bnAluno;



                txtRa.DataBindings.Add("TEXT", bnAluno, "ra_aluno");

                txtNomeAluno.DataBindings.Add("TEXT", bnAluno, "nome_aluno");



                // tbcidade : id_cidade --> tbaluno : cidade_id_cidade



                Cidade Cid = new Cidade();

                dsCidade.Tables.Add(Cid.Listar());



                cbxCidade.DataSource = dsCidade.Tables["TbCidade"];



                //CAMPO QUE SERÁ MOSTRADO PARA O USUÁRIO



                cbxCidade.DisplayMember = "nome_cidade";



                //CAMPO QUE É A CHAVE DA TABELA CIDADE E QUE LIGA COM A TABELA DE ALUNO



                cbxCidade.ValueMember = "id_cidade";



                //No momento de linkar os componentes com o Binding Source linkar também o combox da cidade



                cbxCidade.DataBindings.Add("SelectedValue", bnAluno, "cidade_id_cidade"); // AJUSTAR DROPDOWNSTYLE PARA DropDownList PARA NAO DEIXAR INCLUIR
            }

            catch (Exception ex)

            {
                MessageBox.Show(ex.Message);
            }
        }
Exemple #2
0
        private void btnSalvar_Click(object sender, EventArgs e)

        {
            // validar os dados

            if (txtNomeAluno.Text == "")
            {
                MessageBox.Show("Nome do Aluno inválido!");
            }

            else if (Int32.TryParse(txtRa.Text, out int ra) && ra > 0)

            {
                Aluno RegAlu = new Aluno();



                RegAlu.Raaluno = ra;

                RegAlu.Nomealuno = txtNomeAluno.Text;

                RegAlu.Cidadeidcidade = Convert.ToInt32(cbxCidade.SelectedValue);



                if (bInclusao)

                {
                    if (RegAlu.Salvar() > 0)

                    {
                        MessageBox.Show("Aluno adicionada com sucesso!");



                        btnSalvar.Enabled = false;

                        txtRa.Enabled = false;

                        txtNomeAluno.Enabled = false;

                        cbxCidade.Enabled = false;

                        btnSalvar.Enabled = false;

                        btnAlterar.Enabled = true;

                        btnNovoRegistro.Enabled = true;

                        btnExcluir.Enabled = true;

                        btnCancelar.Enabled = false;



                        bInclusao = false;



                        // recarrega o grid

                        dsAluno.Tables.Clear();

                        dsAluno.Tables.Add(RegAlu.Listar());

                        bnAluno.DataSource = dsAluno.Tables["TBALUNO"];
                    }
                    else

                    {
                        MessageBox.Show("Erro ao gravar aluno!");
                    }
                }
                else

                {
                    if (RegAlu.Alterar() > 0)

                    {
                        MessageBox.Show("Aluno alterado com sucesso!");

                        dsAluno.Tables.Clear();
                        dsAluno.Tables.Add(RegAlu.Listar());
                        txtRa.Enabled = false;

                        txtNomeAluno.Enabled = false;
                        cbxCidade.Enabled    = false;
                        btnSalvar.Enabled    = false;

                        btnAlterar.Enabled      = true;
                        btnNovoRegistro.Enabled = true;
                        btnExcluir.Enabled      = true;

                        btnCancelar.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("Erro ao gravar aluno!");
                    }
                }
            }
            else
            {
                MessageBox.Show("RA Inválido!");
            }
        }