2013-09-25 117 views
3

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

    } 

回答

6

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

cbSupportID.DataSource = ds.Tables[0]; 

或者更好的,应填写数据到DataTable代替DataSet的像这样:

DataTable dt = new DataTable(); 
da.Fill(dt); 
//... 
cbSupportID.DataSource = dt; 
1
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; 

} 

我希望这有助于。

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(); 
     } 
    } 
} 
0
CmbDefaultPrinter.DisplayMember = "[table fieldname]"; 
CmbDefaultPrinter.ValueMember = "[table fieldname]"; 
CmbDefaultPrinter.DataSource = ds.Tables[0]; 
CmbDefaultPrinter.Enabled = true; 
+2

你应该避免发布代码只有答案。 – croxy

0

提及数据字段要加载的列名。

和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" /> 
相关问题