如何从数据库中将数据加载到组合框中

时间:2013-09-25 09:09:47

标签: c# winforms combobox

如何从数据库中将数据加载到组合框中?我想在表单中的组合框中显示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();

    }

5 个答案:

答案 0 :(得分:7)

在这种情况下,DataSource的{​​{1}}应为combobox,请尝试以下操作:

DataTable

或者更好的是,您应该将数据填充到cbSupportID.DataSource = ds.Tables[0]; 而不是DataTable,如下所示:

DataSet

答案 1 :(得分: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;

}

我希望这会有所帮助。

答案 2 :(得分: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();
        }
    }
}

答案 3 :(得分:0)

CmbDefaultPrinter.DisplayMember = "[table fieldname]"; 
CmbDefaultPrinter.ValueMember = "[table fieldname]"; 
CmbDefaultPrinter.DataSource = ds.Tables[0]; 
CmbDefaultPrinter.Enabled = true; 

答案 4 :(得分: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" />