问题描述
我在数据库中有2个表第一个PartyTypes列TypeID,TypeName和
i have 2 tables in database 1st PartyTypes columns TypeID,TypeName and
第二个表派对有PartyID,PartyName,TypeID 。我试图在使用方法的windowsForms party中的组合框中显示TypeName,但我想在数据库中保存TypeID。但是在组合框中它只显示TypeId。
2nd table Parties which has columns PartyID,PartyName,TypeID . i am trying to show TypeName in combo box in windowsForms Parties using a method but i want to save TypeID in database. But in combobox it shows only TypeId.
我想显示TypeName但它应该保存TypeId。我可以这样做。我的代码在下面
i want to display TypeName but it should save TypeId.how can i do it. my code below
//////////////////////////////////// //////////////////////
//////////////////////////////////////////////////////////
void fillcBoxPartyName()//我在组合框中显示值的方法
{
SqlConnection conn = new SqlConnection("Data Source = isolinks; Initial Catalog = WindowsDb; Integrated Security = True");
;   conn.Open();
$
sda = new SqlDataAdapter("从PartyTypes中选择TypeId",conn);
$
   DataTable dt = new DataTable();
sda.Fill(dt);
$
cBoxPType.DataSource = dt;
cBoxPType.ValueMember =" TypeID";
cBoxPType.DisplayMember =" TypeName";
conn.Close();
}
void fillcBoxPartyName() //my method to show values in combo box
{
SqlConnection conn = new SqlConnection("Data Source=isolinks;Initial Catalog=WindowsDb;Integrated Security=True");
conn.Open();
sda = new SqlDataAdapter("select TypeId from PartyTypes", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
cBoxPType.DataSource = dt;
cBoxPType.ValueMember = "TypeID";
cBoxPType.DisplayMember = "TypeName";
conn.Close();
}
////////////////////////////////////// ///
/////////////////////////////////////////
private void FormPartyList_Load(object sender,EventArgs e)
{
fillcBoxPartyName();
private void FormPartyList_Load(object sender, EventArgs e)
{
fillcBoxPartyName();
} //我在formload中使用方法
} // i use method in formload
//////////////////////////
//////////////////////////
请帮助,
推荐答案
这篇关于如何在组合框中显示displaymember但它应该在数据库中保存valuemember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!