C# And MySQL - Bind Combobox Depending On Another Combobox Selected Value
In This C# Tutorial We Will See How To Fill Data Into A ComboBox
Based On A ComboBox Selected value In CSharp Programming Language .
Project Source Code:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='project';username=root;password=");
MySqlCommand command;
MySqlDataAdapter adapter;
DataTable table;
private void Form1_Load(object sender, EventArgs e)
{
string query = "SELECT `CAT_ID`, `CAT_NAME` FROM `categories`";
fillCombo(comboBox1, query, "CAT_NAME", "CAT_ID");
comboBox1_SelectedIndexChanged(null, null);
}//create a methode to Populate a Combobox From Mysql Database
public void fillCombo(ComboBox combo, string query, string displayMember, string valueMember)
{
command = new MySqlCommand(query,con);
adapter = new MySqlDataAdapter(command);
table = new DataTable();
adapter.Fill(table);
combo.DataSource = table;
combo.DisplayMember = displayMember;
combo.ValueMember = valueMember;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int val;
Int32.TryParse(comboBox1.SelectedValue.ToString(), out val);
string query = "SELECT `ID_PRO`, `PRO_NAME`, `QTE_IN_STOCK`, `PRICE`, `ID_CAT` FROM `products` WHERE ID_CAT = "+ val;
fillCombo(comboBox2, query, "PRO_NAME", "ID_PRO");
}
}
}///////////////OUTPUT:
Related Posts:
Connect C# To MySQL And Display Data
Bagikan
C# And MySQL - Populate Combobox Depending On Another Combobox Selected Value
4/
5
Oleh
insurance