簡體   English   中英

如何從數據庫中將數據加載到組合框中

[英]How do I load data into combobox from database

如何從數據庫中將數據加載到組合框中? 我想在表單中的組合框中顯示supportID。 我正在使用的代碼粘貼在這里。 我在formload中調用BindData()。 我得到例外:無法綁定到新的顯示成員。 參數名稱:newDisplayMember。 我用的代碼是:

public void BindData()
    {
        SqlConnection con = new SqlConnection(@"server=RSTT2; database = Project ;  User Id=sa; Password=PeaTeaCee5#");
        con.Open();
        string strCmd = "select supportID from Support";
        SqlCommand cmd = new SqlCommand(strCmd, con);
        SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cbSupportID.DataSource = ds;
        cbSupportID.DisplayMember = "supportID";
        cbSupportID.ValueMember = "supportID";
        cbSupportID.Enabled = true;
        cmd.ExecuteNonQuery();
        con.Close();

    }

在這種情況下, comboboxDataSource應該是DataTable ,試試這個:

cbSupportID.DataSource = ds.Tables[0];

或者更好的是,您應該將數據填充到DataTable而不是像這樣的DataSet

DataTable dt = new DataTable();
da.Fill(dt);
//...
cbSupportID.DataSource = dt;
public void BindData()
{
    SqlConnection con = new SqlConnection(@"server=RSTT2; database = Project ;  User Id=sa; Password=PeaTeaCee5#");
    con.Open();
    string strCmd = "select supportID from Support";
    SqlCommand cmd = new SqlCommand(strCmd, con);
    SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    cmd.ExecuteNonQuery();
    con.Close();

    cbSupportID.DisplayMember = "supportID";
    cbSupportID.ValueMember = "supportID";       
    cbSupportID.DataSource = ds;

    cbSupportID.Enabled = true;

}

我希望這有幫助。

請遵循以下示例:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            string conStr = @"Server =.\SQLEXPRESS2014; Database=NORTHWND; User Id=sa; Password=******";
            SqlConnection conn = new SqlConnection(conStr);
            DataSet ds = new DataSet();
            string getEmpSQL = "SELECT E.LastName FROM dbo.Employees E;";
            SqlDataAdapter sda = new SqlDataAdapter(getEmpSQL, conn);

            try
            {
                conn.Open();
                sda.Fill(ds);
            }catch(SqlException se)
            {
                MessageBox.Show("An error occured while connecting to database" + se.ToString());
            }
            finally
            {
                conn.Close();
            }

            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = ds.Tables[0].Columns[0].ToString();
        }
    }
}
CmbDefaultPrinter.DisplayMember = "[table fieldname]"; 
CmbDefaultPrinter.ValueMember = "[table fieldname]"; 
CmbDefaultPrinter.DataSource = ds.Tables[0]; 
CmbDefaultPrinter.Enabled = true; 

提及要加載的DataField中的列名稱。

並在頁面加載的aspx.cs中綁定gridview。

enter code here
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"  />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="User_Group" HeaderText="UserName" ItemStyle 
Width="150px" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM