Next and Previous button problem

Damage

Member
Joined
Sep 5, 2016
Messages
6
Location
Serbia
Programming Experience
3-5
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.

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:
You're doing it all wrong I'm afraid. You've already got a BindingSource so you should just be using that. Bind the BindingSource to your grid AND your other controls, e.g.
myDataGridView.DataSource = myBindingSource;
myTextBox.DataBindings.Add("Text", myBindingSource, "MyColumn");
To navigate through the records you simply call MoveNext or MovePrevious on the BindingSource. Any changes you make to the data are pushed down to the DataTable and you save them to the database by calling Update on your data adapter.
 
Back
Top Bottom