是否可以将数据形成列表框中显示的表格? [英] is it possible to have data form a table shown in a list box?

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

问题描述

因为问题状态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?

推荐答案

请参见此处: https://msdn.microsoft.com/en-us/library/w67sdsex(v = VS.90)的.aspx [ ^ ]


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();
        }

      
           
    }
}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆