Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Datagridview is not refreshing after i click save

$
0
0

Good day Ma'am/Sir

I have a problem when it comes in Datagridview, After I click the save button the datagrid is not showing but the data I inputed was saved on MySQL database how can I fix this?

thanks

Here's my Code:

mydbs CLASS:

public static DataSet fetchRecords(String sqlstr)
        {
            try
            {
                openDB();
                MySqlDataAdapter da = new MySqlDataAdapter(sqlstr, dbconn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Data");
                return ds;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                closeDB();
            }

        }

Alumnus CLASS:

public static DataSet retrieve()
        {
            try
            {
                String sqlstr = "SELECT * FROM graduated_student";
                return mydbs.fetchRecords(sqlstr);
            }
            catch (Exception e)
            {
                return null;
            }
        }
        public static DataSet retrieve(String criteria)
        {
            try
            {
                String sqlstr = "SELECT * FROM graduated_student WHERE " + criteria;
                return mydbs.fetchRecords(sqlstr);
            }
            catch(Exception e)
            {
                return null;
            }
        }

ACP CLASS:

private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text.Equals("&Add"))
            {
                tbName.Enabled = true;
                tbAddress.Enabled = true;
                tbPBirth.Enabled = true;
                tbDBirth.Enabled = true;
                tbCitizenship.Enabled = true;
                btnDelete.Enabled = false;
                btnEdit.Enabled = false;
                tbName.Clear();
                tbAddress.Clear();
                tbPBirth.Clear();
                tbDBirth.Clear();
                tbCitizenship.Clear();
                btnAdd.Text = "Save";
                btnEdit.Text = "Cancel";
            }
            else
            {
                if (tbName.Text.Equals(""))
                {
                    MessageBox.Show("Please type the name of Alumnus");
                    tbName.Focus();
                }
                else if (tbAddress.Text.Equals(""))
                {
                    MessageBox.Show("Please type the current address of Alumnus");
                    tbAddress.Focus();
                }
                else if (tbPBirth.Text.Equals(""))
                {
                    MessageBox.Show("Please type the place of birth");
                    tbPBirth.Focus();
                }
                else if (tbDBirth.Text.Equals(""))
                {
                    MessageBox.Show("Please type the date of birth");
                    tbDBirth.Focus();
                }
                else if (tbCitizenship.Text.Equals(""))
                {
                    MessageBox.Show("Please type the citizenship");
                    tbCitizenship.Focus();
                }
                else
                {
                    Alumnus a = new Alumnus(tbName.Text, tbAddress.Text, tbPBirth.Text, tbDBirth.Text, tbCitizenship.Text);
                    String msg = a.save();
                    MessageBox.Show(msg, "Save Alumnus Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    tbName.Enabled = false;
                    tbAddress.Enabled = false;
                    tbPBirth.Enabled = false;
                    tbDBirth.Enabled = false;
                    tbCitizenship.Enabled = false;
                    btnAdd.Text = "&Add";
                    btnEdit.Text = "&Edit";
                    btnDelete.Enabled = true;
                    btnEdit.Enabled = true;
                    ACP_Load(this, null);
                }
            }
        }

        private void ACP_Load(object sender, EventArgs e)
        {
            try
            {
                dataAlumnus.DataSource = Alumnus.retrieve().Tables[0];
                dataAlumnus.Columns[0].HeaderText = "Alumnus ID";
                dataAlumnus.Columns[1].HeaderText = "Name";
                dataAlumnus.Columns[2].HeaderText = "Current Address";
                dataAlumnus.Columns[3].HeaderText = "Place of Birth";
                dataAlumnus.Columns[4].HeaderText = "Date of Birth";
                dataAlumnus.Columns[5].HeaderText = "Citizenship";
                dataAlumnus_CellContentClick(this, null);
                dataAlumnus.Refresh();

                
            }
            catch
            {

            }
            
        }


Viewing all articles
Browse latest Browse all 2535

Trending Articles