我们如何在c#window应用程序的comboBox中显示数据库表一列值 [英] How we display database table one column values in comboBox in c# window application

查看:106
本文介绍了我们如何在c#window应用程序的comboBox中显示数据库表一列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

告诉我任何正文如何在c#window application中的comboBox中显示数据库表的一个列值。我们使用了这段代码,但它的工作不正确..plz帮助

Tell me any body how we display database table one column values in comboBox in c# window application.we used this code but its not work corret..plz help

private void bindCombo()
         {
             string q = "select policestation from addpolicestation";
             SqlDataAdapter da = new SqlDataAdapter(q, "da.crim()");
             DataTable dt = new DataTable();

             da.Fill(dt);

             comboB1.Items.Add("--Select--");
             foreach (DataRow row in dt.Rows)
             {
                 comboB1.Items.Add(row["policestation"]);

             }
             comboB1.SelectedIndex = 0;
         }

推荐答案

我找到了这个解决方案...

i have find this solution...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        SqlDataAdapter da;
        DataSet ds = new DataSet();

        private void Form1_Load(object sender, EventArgs e)
        {
            // Build the Connection String and create a SQL Connection object
            String cnnStr = "Data Source = localhost; Initial Catalog = Northwind; Integrated Security = SSPI";
            SqlConnection cnn = new SqlConnection(cnnStr);
            // Create a SQL Command object from the connection object and setup the SELECT command
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Customers";
            // Create the data adapter to use to get the data and handle connecting to the DB
            da = new SqlDataAdapter(cmd);
            // Get the data from the database. I am using the Northwind database the customers table
            da.Fill(ds, "Customers");
            // Disconnect the event handler to handle the comboBox1_SelectedIndexChanged event
            // while the combo box is being populated, otherwise it will fire a couple of times
            // before you are ready to handle them.
            this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // Connect the cobo box to the data source from where the data is comming from
            // In this case the Customers data table in the dataset.
            comboBox1.DataSource = ds.Tables["Customers"];
            // Tell the combo box what collumn to display to the user
            comboBox1.DisplayMember = "CompanyName";
            // Tell the combo box what collumn to use with the displayed value, value is not displayed
            comboBox1.ValueMember = "CustomerID";
            // Restored the event handler
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // When the user clicks on a item in the combo box ist primary key will be displayed.
            MessageBox.Show("Customer Primary Key is \"" + comboBox1.SelectedValue + "\"");
        }
    }
}





享受......



enjoy...


您好发布之前请首先查询您的任何疑问



查看此内容,



< a href =http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C>通过C#访问SQL Server的初学者指南 [ ^ ]
Hi please google first for any of your queries before posting

Check this,

Beginners guide to accessing SQL Server through C#[^]


结合LInQ和ComboBox ItemSource属性,我们可以轻松实现以下功能:



With the combination of LInQ and ComboBox ItemSource property, we can easily achieve the functionality as:

public List<string> GetPoliceStation()
        {
            List<string> retStation = new List<string>();
            using (DBEntities dbContext = new DBEntities(connectionString))
            {
                return DBEntities.AddPoliceStation.ToList();
            }
        }


comboB1.ItemsSource = GetPoliceStation();
comboB1.DisplayMemberPath = "PoliceStation";
comboB1.SelectedValuePath = "PoliceStation";
comboB1.SelectedIndex = 0;</string></string></string>


这篇关于我们如何在c#window应用程序的comboBox中显示数据库表一列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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