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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19000713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:51:21  来源:igfitidea点击:

How do I load data into combobox from database

c#winformscombobox

提问by karthik reddy

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:

如何将数据从数据库加载到组合框?我想在表单的组合框中显示 supportID。我正在使用的代码粘贴在这里。我在表单加载中调用 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();

    }

回答by King King

The DataSourcefor your comboboxshould be a DataTablein this case, try this:

DataSourcecombobox应该是DataTable在这种情况下,试试这个:

cbSupportID.DataSource = ds.Tables[0];

Or better, you should fill data into a DataTableinstead of a DataSetlike this:

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

DataTable dt = new DataTable();
da.Fill(dt);
//...
cbSupportID.DataSource = dt;

回答by Rafay

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;

}

I hope this helps.

我希望这有帮助。

回答by Ashraf Abusada

Follow this example:

按照这个例子:

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

回答by user6926813

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

回答by ayush agarwal

Mention the Column Name in DataField which you want to load.

在要加载的 DataField 中提及列名称。

and in aspx.cs in page load bind the gridview.

并在页面加载中的 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" />