C# How To Use BindingManagerBase And MySql CommandBuilder
In This C# Tutorial We Will See How To Use BindingManagerBase To Insert Update Delete Search And Navigate Data From Database Using CSharp Programming Language And MySQL Database.
Part 1
Part 2
Source Code :
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_And_MySQL
{
public partial class Using_DataBindings : Form
{
public Using_DataBindings()
{
InitializeComponent();
}
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog ='test_db';username=root;password=");
MySqlCommand command;
MySqlDataAdapter adapter;
DataTable table = new DataTable();
BindingManagerBase managerBase;
MySqlCommandBuilder builder;
private void Using_DataBindings_Load(object sender, EventArgs e)
{
string query = "SELECT * FROM users";
command = new MySqlCommand(query, connection);
adapter = new MySqlDataAdapter(command);
adapter.Fill(table);
dataGridView1.DataSource = table;
textBoxID.DataBindings.Add("text",table,"id");
textBoxFName.DataBindings.Add("text", table, "fname");
textBoxLName.DataBindings.Add("text", table, "lname");
textBoxAge.DataBindings.Add("text", table, "age");
managerBase = this.BindingContext[table];
}
private void BTN_FIRST_Click(object sender, EventArgs e)
{
managerBase.Position = 0;
}
private void BTN_NEXT_Click(object sender, EventArgs e)
{
managerBase.Position += 1;
}
private void BTN_PREVIOUS_Click(object sender, EventArgs e)
{
managerBase.Position -= 1;
}
private void BTN_LAST_Click(object sender, EventArgs e)
{
managerBase.Position = managerBase.Count;
}
private void BTN_NEW_Click(object sender, EventArgs e)
{
managerBase.AddNew();
}
private void BTN_INSERT_Click(object sender, EventArgs e)
{
managerBase.EndCurrentEdit();
builder = new MySqlCommandBuilder(adapter);
adapter.Update(table);
MessageBox.Show("New User Added","New User",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void BTN_UPDATE_Click(object sender, EventArgs e)
{
managerBase.EndCurrentEdit();
builder = new MySqlCommandBuilder(adapter);
adapter.Update(table);
MessageBox.Show("User Data Updated", "Edit User", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void BTN_DELETE_Click(object sender, EventArgs e)
{
managerBase.RemoveAt(managerBase.Position);
builder = new MySqlCommandBuilder(adapter);
adapter.Update(table);
MessageBox.Show("User Deleted", "Remove User", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_And_MySQL
{
public partial class Using_DataBindings : Form
{
public Using_DataBindings()
{
InitializeComponent();
}
MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog ='test_db';username=root;password=");
MySqlCommand command;
MySqlDataAdapter adapter;
DataTable table = new DataTable();
BindingManagerBase managerBase;
MySqlCommandBuilder builder;
private void Using_DataBindings_Load(object sender, EventArgs e)
{
string query = "SELECT * FROM users";
command = new MySqlCommand(query, connection);
adapter = new MySqlDataAdapter(command);
adapter.Fill(table);
dataGridView1.DataSource = table;
textBoxID.DataBindings.Add("text",table,"id");
textBoxFName.DataBindings.Add("text", table, "fname");
textBoxLName.DataBindings.Add("text", table, "lname");
textBoxAge.DataBindings.Add("text", table, "age");
managerBase = this.BindingContext[table];
}
private void BTN_FIRST_Click(object sender, EventArgs e)
{
managerBase.Position = 0;
}
private void BTN_NEXT_Click(object sender, EventArgs e)
{
managerBase.Position += 1;
}
private void BTN_PREVIOUS_Click(object sender, EventArgs e)
{
managerBase.Position -= 1;
}
private void BTN_LAST_Click(object sender, EventArgs e)
{
managerBase.Position = managerBase.Count;
}
private void BTN_NEW_Click(object sender, EventArgs e)
{
managerBase.AddNew();
}
private void BTN_INSERT_Click(object sender, EventArgs e)
{
managerBase.EndCurrentEdit();
builder = new MySqlCommandBuilder(adapter);
adapter.Update(table);
MessageBox.Show("New User Added","New User",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void BTN_UPDATE_Click(object sender, EventArgs e)
{
managerBase.EndCurrentEdit();
builder = new MySqlCommandBuilder(adapter);
adapter.Update(table);
MessageBox.Show("User Data Updated", "Edit User", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void BTN_DELETE_Click(object sender, EventArgs e)
{
managerBase.RemoveAt(managerBase.Position);
builder = new MySqlCommandBuilder(adapter);
adapter.Update(table);
MessageBox.Show("User Deleted", "Remove User", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}