3

How do I load data into combobox from database? I want to display the supportID into the combobox in the form. the code I am using is pasted here. I am calling BindData() in the formload. Ia m getting exception as: Cannot bind to the new display member. Parameter name: newDisplayMember. the code I used is:

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

    }
4

5 回答 5

7

在这种情况下,DataSource你的combobox应该是 a DataTable,试试这个:

cbSupportID.DataSource = ds.Tables[0];

或者更好的是,您应该将数据填充到 aDataTable而不是DataSet这样的:

DataTable dt = new DataTable();
da.Fill(dt);
//...
cbSupportID.DataSource = dt;
于 2013-09-25T09:16:23.883 回答
2
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;

}

我希望这有帮助。

于 2013-09-25T09:16:39.160 回答
0
CmbDefaultPrinter.DisplayMember = "[table fieldname]"; 
CmbDefaultPrinter.ValueMember = "[table fieldname]"; 
CmbDefaultPrinter.DataSource = ds.Tables[0]; 
CmbDefaultPrinter.Enabled = true; 
于 2016-10-05T13:59:02.687 回答
0

按照这个例子:

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();
        }
    }
}
于 2015-12-04T18:57:23.170 回答
0

在要加载的 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" />
于 2017-11-24T10:03:43.273 回答