C# - How To Fill Data Into DataGridView Depending On A Combobox Selected value
In This C# Tutorial We Will See How To Bind Data Into A DataGridView
Based On A Combobox Selected value In CSharp Programming Language And MySQL Database .
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 System.IO;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
MySqlConnection con = new MySqlConnection("datasource=localhost;port=3306;Initial Catalog='project';username=root;password=");
MySqlCommand command;
MySqlDataAdapter adapter;
DataTable table;
private void Form2_Load(object sender, EventArgs e)
{
string query = "SELECT `CAT_ID`, `CAT_NAME` FROM `categories`";
comboBox1.DataSource = getData(query);
comboBox1.DisplayMember = "CAT_NAME";
comboBox1.ValueMember = "CAT_ID";
comboBox1_SelectedIndexChanged(null, null);
}
public DataTable getData(string query)
{
command = new MySqlCommand(query, con);
adapter = new MySqlDataAdapter(command);
table = new DataTable();
adapter.Fill(table);
return table;
}
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;
dataGridView1.DataSource = getData(query);
}
}
}
///////////////OUTPUT:
Bind DataGridView Depending On A Combobox value |
Related Posts:
Connect C# To MySQL And Display Data
Bagikan
C# And MySQL - How To Populate DataGridView Depending On A Combobox Selected value
4/
5
Oleh
insurance