hello guys how can i update datagridview to display the newly added data to the database

here is my sample code for adding data to the database

MySqlCommand cmdAdd = new MySqlCommand("INSERT INTO tblelemdetails (STUD_ID,LNAME,FMNAME,AGE,ADDRESS,BDAY,BPLACE,RELIGION,CITIZEN,CITY,LSCHOOL,LSCHOOLADD,SCHOOLYEAR,GENDER,LEVEL) VALUES('" + Ids.ToString() + "','" + txtLName.Text + "','" + fnamer + "','" + txtAge.Text + "','" + txtHAddress.Text + "','" + bdayer + "','" + txtPOBirth.Text + "','" + txtReligion.Text + "','" + txtCitizen.Text + "','" + txtCity.Text + "','" + txtLSchool.Text + "','" + txtLSchoolAdd.Text + "','" + txtSY.Text + "','" + cmbGender.Text + "','" + cmbYrLvl.Text + "')", db_con.DbConn);
                cmdAdd.Transaction = mytrans;
                cmdAdd.ExecuteNonQuery();
                cmdAdd.Dispose();
                mytrans.Commit();
                MessageBox.Show("Data Has Been Succesfully Added","Important Note",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                Clear();
                db_con.DbConn.Close();

pls need some help and thanks

You will either need to re-select the data from the database OR put the inserted rows into a class or other container (like a GridViewRow) that is the same shape as your GridView and add it to the GridView.

yes just do datagridview1.clear() then do what you did to populate it the first time round. i think that works . . .

Edited 4 Years Ago by ChrisHunter: n/a

i applied repopulate the datagridview but it doesn't work.

Does this mean you tried the .Clear() method of the data grid view and following that used the populated the grid view the same way you had in the originally ?

yup, i already used that kind of approach but it does not update the datagridview?

This article has been dead for over six months. Start a new discussion instead.