我有三个控件:comboBox,button,listBox和一个spl2000建的表(通讯录)我想实现在comboBox显示(通讯录)的某列,而在单击button后,在listBox显示参照comboBox值(通讯录)中的某行,我该怎么实现。
特别是我要在button里的click写什么代码才能按comboBox提供的名字找到该名字所在表(通讯录)中有关该名字的所有信息,并把信息显示在listbox上。数据库要与哪些控件绑定。

解决方案 »

  1.   

    你可以使用combobox的绑定,然后用displaymember显示name字段信息,然后用valuemember表明id字段信息。这样你就可以在button事件中获得ID,就可以方便进行显示。
      

  2.   

    同意楼上的做法,就是先将comboBox绑定,然后在button中写:
    1。查找comboBox所选的名字的id号
    2。根据id号查找数据库中此id号的信息,然后显示在listBox中
      

  3.   


    那button中要写什么代码才能查找到comboBox中所选的id号并显示出来呢,我是新手,不懂,麻烦高人写个试例让我参考一下。先谢了。
      

  4.   

    to 那button中要写什么代码才能查找到comboBox中所选的id号并显示出来呢,我是新手,不懂,麻烦高人写个试例让我参考一下。先谢了。Use "ComboBox.SelectedValue" to get the current selected value
      

  5.   

    设置comboBox的Text属性显示通讯录的某列,Value属性用来存放id(也就是通讯录的主键)将来在其他地方使用的时候,根据Value属性再找到对应的纪录
      

  6.   

    //给一段示例代码using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace DataBindDemo
    {
    public class DropDownList : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnClick;
    protected System.Web.UI.WebControls.DropDownList dropAuthors;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (! IsPostBack ) 
    {
    SqlConnection conPubs;
    SqlCommand cmdSelect;
    SqlDataReader dtrAuthors; conPubs = new SqlConnection( @"Server=localhost;uid=sa;pwd=sa;Database=Pubs" );
    conPubs.Open();
    cmdSelect = new SqlCommand( "Select au_id, au_lname From Authors", conPubs );
    dtrAuthors = cmdSelect.ExecuteReader(); dropAuthors.DataSource = dtrAuthors;
    dropAuthors.DataTextField = "au_lname";
    dropAuthors.DataValueField = "au_id";
    dropAuthors.DataBind(); dtrAuthors.Close();
    conPubs.Close();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {    
    this.btnClick.Click += new System.EventHandler(this.btnClick_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnClick_Click(object sender, System.EventArgs e)
    {
    Response.Write("作者的id为:" + dropAuthors.SelectedValue);
    Response.Write("<br>");
    Response.Write("作者的姓名为:" + dropAuthors.SelectedItem.Text);
    }
    }
    }