绑定具有两列的表中的组合框
本文关键字:组合 两列 绑定 | 更新日期: 2023-09-27 18:35:53
如何连接这些数据,或者有更好的方法?
数据联接accountID - accountname
时的示例
public void BindData()
{
cn.Open();
string strCmd = "select accountID from MsAccount";
string strCmd2 = "select accountName from MsAccount";
SqlCommand cmd = new SqlCommand(strCmd, cn);
SqlCommand cmd2 = new SqlCommand(strCmd2, cn);
SqlDataAdapter da = new SqlDataAdapter(strCmd, cn);
SqlDataAdapter da2 = new SqlDataAdapter(strCmd2, cn);
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
da.Fill(dt);
da2.Fill(dt2);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "AccountID";
comboBox1.DataSource = dt2;
comboBox1.ValueMember = "accountName";
comboBox1.DisplayMember = "AccountID"+ "-"+"accountName";<<OUTPUT that i need
//comboBox1.DisplayMember = "accountName";
comboBox1.Enabled = true;
comboBox2.Enabled = true;
cmd.ExecuteNonQuery();
cn.Close();
}
您可以使用
SELECT
accountID,
(CAST(accountID AS VARCHAR(10))+'-'+accountName) AS accountName
FROM
MsAccount
而是单独称呼他们。
public void BindData()
{
cn.Open();
string strCmd = "SELECT accountID, (CAST(accountID AS VARCHAR(10))+'-'+accountName) AS accountName FROM MsAccount";
SqlCommand cmd = new SqlCommand(strCmd, cn);
SqlDataAdapter da = new SqlDataAdapter(strCmd, cn);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "accountID";
comboBox1.DisplayMember = "accountName";
comboBox1.Enabled = true;
cn.Close();
}