![](/img/trans.png)
[英]Filtering data in DataTable/DataGridView by change in selected item of ComboBox
[英]Retrieving data that selected in combobox to datagridview
我如何檢索在組合框中選擇的數據以及如何從數據庫到datagridview檢索該數據的信息
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=acc;uid=sa;pwd=emotions");
private void Form1_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select ClassId, Class from Class order by ClassId", cn);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Class";
comboBox1.ValueMember = "ClassId";
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("select SNAme, FName, SPhoneNo from Students where ClassId =" + comboBox1.SelectedValue, cn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.