Hello all.
I have problem with Next and Previous button. When i delete or save my data it show in dataGridView but it dont show in my textBoxes when i shift next and previous buttons.
I need to load form agen after save or delete some how but i dont know how. I post my code in here so please help me and post any code that can help me.
Thank you all.
I have problem with Next and Previous button. When i delete or save my data it show in dataGridView but it dont show in my textBoxes when i shift next and previous buttons.
I need to load form agen after save or delete some how but i dont know how. I post my code in here so please help me and post any code that can help me.
Thank you all.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace mitAdministrator { public partial class Katedre : Form { MySqlConnection connection = new MySqlConnection("SERVER=*********;DATABASE=sqlmasdata;UID=*******;PASSWORD=*******;"); MySqlDataAdapter adapter; DataTable table = new DataTable(); int pos = 0; public Katedre() { InitializeComponent(); } private void buttonKatedreExit_Click(object sender, EventArgs e) { this.Close(); } public void showData(int index) { textBoxIndentifikacioniBroj.Text = table.Rows[index][0].ToString(); textBoxImeKatedre.Text = table.Rows[index][1].ToString(); } private void Katedre_Load(object sender, EventArgs e) { //Populate textBox adapter = new MySqlDataAdapter("SELECT * FROM mitAdminPanelKatedre", connection); adapter.Fill(table); showData(pos); //Disable textBox textBoxIndentifikacioniBroj.Enabled = false; textBoxImeKatedre.Enabled = false; // Load Data Grid View loadTable(); } public void loadTable() { MySqlCommand cmdDataBase = new MySqlCommand(" select * from mitAdminPanelKatedre ;", connection); try { MySqlDataAdapter sda = new MySqlDataAdapter(); sda.SelectCommand = cmdDataBase; DataTable dbDataSet = new DataTable(); sda.Fill(dbDataSet); BindingSource bSource = new BindingSource(); bSource.DataSource = dbDataSet; dataGridViewKatedre.DataSource = bSource; sda.Update(dbDataSet); } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void buttonKatedreSave_Click(object sender, EventArgs e) { try { if (textBoxIndentifikacioniBroj.Text != "" && textBoxImeKatedre.Text != "") { connection.Open(); string query = "insert into mitAdminPanelKatedre (katedraID,katedraIme) values (@katedraID,@katedraIme)"; using (MySqlCommand cmd = new MySqlCommand(query, connection)) { cmd.Parameters.AddWithValue("@katedraID", textBoxIndentifikacioniBroj.Text); cmd.Parameters.AddWithValue("@katedraIme", textBoxImeKatedre.Text); cmd.ExecuteNonQuery(); } connection.Close(); MessageBox.Show("Katedra " + textBoxImeKatedre.Text + "je uspe?no dodata u bazu!", "Uspesno insertovanje podataka.", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message); } // Lock textBox textBoxIndentifikacioniBroj.Enabled = false; textBoxImeKatedre.Enabled = false; // Unlock button buttonKatedreNext.Enabled = true; buttonKatedrePrevious.Enabled = true; // Refresh Data Grid View loadTable(); //Refresh textBox textBoxIndentifikacioniBroj.Refresh(); textBoxImeKatedre.Refresh(); this.Refresh(); } private void buttonKatedreNew_Click(object sender, EventArgs e) { textBoxIndentifikacioniBroj.Enabled = true; textBoxImeKatedre.Enabled = true; textBoxIndentifikacioniBroj.Clear(); textBoxImeKatedre.Clear(); buttonKatedreNext.Enabled = false; buttonKatedrePrevious.Enabled = false; } private void buttonKatedreNext_Click(object sender, EventArgs e) { pos++; if (pos < table.Rows.Count) { showData(pos); } else { pos = table.Rows.Count - 1; } } private void buttonKatedrePrevious_Click(object sender, EventArgs e) { pos--; if (pos >= 0) { showData(pos); } else { pos = 0; textBoxIndentifikacioniBroj.Text = table.Rows[0][0].ToString(); textBoxImeKatedre.Text = table.Rows[0][1].ToString(); } } private void buttonKatedreDelete_Click(object sender, EventArgs e) { string query = "delete from mitAdminPanelKatedre where katedraID='" + textBoxIndentifikacioniBroj.Text + "' and katedraIme='" +textBoxImeKatedre.Text+ "'"; MySqlCommand cmdDataBase = new MySqlCommand(query, connection); MySqlDataReader myReader; try { connection.Open(); myReader = cmdDataBase.ExecuteReader(); MessageBox.Show("Katedra " + textBoxImeKatedre.Text + "je uspe?no obrisana!", "Uspesno brisanje podataka.", MessageBoxButtons.OK, MessageBoxIcon.Information); while (myReader.Read()) { } myReader.Close(); connection.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { textBoxIndentifikacioniBroj.Clear(); textBoxImeKatedre.Clear(); } loadTable(); } } }
Last edited by a moderator: