2013-09-25 148 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" /> 
相關問題