简体   繁体   English

将组合框中选择的数据检索到datagridview

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM