0

我如何检索在组合框中选择的数据并将该数据的信息从数据库检索到 datagridview

4

1 回答 1

0
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;
        }
    }
}

在此处输入图像描述

于 2013-03-02T23:08:14.330 回答