public void load_dataGridView1() { try { Cursor.Current = Cursors.WaitCursor; //BindingSource bindingsource = new BindingSource(); dataGridView1.Rows.Clear(); List <DBstuinfo> rec = DBstuinfo.GetData(); foreach (DBstuinfo data in rec) { string[] row = new string[] { data.Id.ToString(), data.FirstName, data.LastName, data.MiddleName, data.Course, data.Year.ToString(), data.Address }; dataGridView1.Rows.Add(row); dataGridView1.Refresh(); } // bindingsource.Add(data); //this.dataGridView1.Refresh(); //this.dataGridView1.AutoGenerateColumns = false; //this.dataGridView1.DataSource = bindingsource; //this.dataGridView1.ClearSelection(); Cursor.Current = Cursors.Default; } catch (Exception ex) { ex.ToString(); } }
public static List <DBstuinfo> GetData() { List <DBstuinfo> data = new List <DBstuinfo>(); MySqlConnection con = stuinfocon.ConnectDatabase(); try { MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + tablename, con); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { DBstuinfo rawData = new DBstuinfo(); rawData.Id = reader.GetInt32(0); rawData.FirstName = reader.GetString(1); rawData.LastName = reader.GetString(2); rawData.MiddleName = reader.GetString(3); rawData.Course = reader.GetString(4); rawData.Year = reader.GetInt32(5); rawData.Address = reader.GetString(6); data.Add(rawData); } } reader.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } return(data); }
public static DBstuinfo GetDataID(int id) { DBstuinfo data = null; MySqlConnection con = stuinfocon.ConnectDatabase(); try { MySqlCommand cmd = new MySqlCommand("SELECT * FROM " + tablename + " WHERE id=" + id, con); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); data = new DBstuinfo(); data.Id = reader.GetInt32(0); data.FirstName = reader.GetString(1); data.LastName = reader.GetString(2); data.MiddleName = reader.GetString(3); data.Course = reader.GetString(4); data.Year = reader.GetInt32(5); data.Address = reader.GetString(6); } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); MessageBox.Show(ex.ToString()); } finally { con.Close(); } return(data); }
private void buttonS_Click(object sender, EventArgs e) { enAbleButton(); if (isAdd == true) { DBstuinfo add = new DBstuinfo(textBoxF.Text, textBoxL.Text, textBoxM.Text, comboBoxC.Text, Convert.ToInt32(comboBoxY.Text), textBoxA.Text); add.Add(); load_dataGridView1(); MessageBox.Show("Added successfuly"); } else if (isAdd == false) { try { int idx = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString()); DBstuinfo cards = DBstuinfo.GetDataID(idx); cards.FirstName = textBoxF.Text; cards.LastName = textBoxL.Text; cards.MiddleName = textBoxM.Text; cards.Course = comboBoxC.Text; cards.Year = Convert.ToInt32(comboBoxY.Text); cards.Address = textBoxA.Text; cards.Update(); load_dataGridView1(); MessageBox.Show("Edited successfuly"); } catch (Exception ex) { MessageBox.Show("Error updating record " + ex.Message); } } }
private void buttonD_Click(object sender, EventArgs e) { try { int idx = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString()); DBstuinfo cards = DBstuinfo.GetDataID(idx); if (cards != null) { DialogResult dialogResult = MessageBox.Show(" Do you want to delete this record?", "Delete", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { cards.Delete(); load_dataGridView1(); } } } catch (Exception ex) { MessageBox.Show("Error deleting record " + ex.Message); } }