本文介绍了是否可以将数据形成列表框中显示的表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为问题状态id想知道它是否可以在列表框中显示sql表的数据?如果您将列表框与数据库链接并在文本框中显示值(如果在ListBox中选中)?

hi as the questions states id like to know if its possible to show data fom a sql table in a list box? and if you Link a List Box with Database and show values in a textbox if selected in ListBox?

推荐答案


using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet Ds = new DataSet();
          

            string strConnectionString = "Data Source=localhost,3601;Network   
            Library=DBMSSOCN;Initial Catalog=empdb;User ID=myusername;Password=mypwd;";
            SqlConnection objconnection = new SqlConnection(strConnectionString);
            using (SqlCommand cmd = new SqlCommand("SELECT  [empcode] FROM [empdb]", 
            objconnection))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(Ds);
                }
            }

            var empList = Ds.Tables[0].AsEnumerable().Select(dataRow =>      
            dataRow.Field<int>("empcode")).ToList();
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.DataSource = empList;

          
            comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = comboBox1.SelectedItem.ToString();
        }

      
           
    }
}


这篇关于是否可以将数据形成列表框中显示的表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:05