本文介绍了显示计划名称但保留计划ID并在组合框中检索ID的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多这方面的文章,但到目前为止还没有任何帮助。

我的ComboBox名称是cbPlan。我想在它的显示中检索PlanName,但是想要实际保存PlanID。

以下代码显示名称和ID。我尝试过ValueMember,DisplayMember,属性,但还是无法对其进行排序。

最后,即使这样做,我如何在另一个表中插入PlanID?我会使用Convert.ToString(cbPlan.Text) - 它会带来PlanName而不是ID。

请帮忙 - 非常感谢你提前! :)

P.S. PlanID的数据类型为int。





I saw many many articles on this but none helped so far.
My ComboBox name is cbPlan. I want to Retrieve PlanName in it's display but want to actually hold it PlanID.
Following code displays both Names and IDs. I tried ValueMember, DisplayMember, properties but couldn't get it sorted yet.
Finally, even if this works out, how will I get to insert PlanID in another table? Will i use Convert.ToString(cbPlan.Text) - which would bring the PlanName and not the ID.
Please help on this - A big thank you in advance! :)
P.S. PlanID's data type is int.


private void cbPlan_Click(object sender, EventArgs e)
       {
           cbPlan.Items.Clear();

           string pullsub = "select PlanID,PlanName from fbkPlanMaster(nolock)";
           string connString = ConfigurationManager.ConnectionStrings["Dbconn"].ToString();
           SqlConnection connection = new SqlConnection(connString); // defining sql connection
           SqlCommand cmd = new SqlCommand(pullsub, connection);
           cmd.CommandText = pullsub;
           connection.Open();
           SqlDataReader drd = cmd.ExecuteReader();
           while (drd.Read())
           {

               cbPlan.Items.Add(drd["PlanID"]);
               cbPlan.Items.Add(drd["PlanName"]);
               cbPlan.ValueMember = "PlanID";
               cbPlan.DisplayMember = "PlanName";


           }
       }





我是什么尝试过:



- 类似行的几篇文章。

- 上面显示的代码。

- ValueMember和DisplayMember属性。

- 转换为int。



What I have tried:

- Several articles on similar lines.
- Code as displayed above.
- ValueMember and DisplayMember properties.
- Conversion into int.

推荐答案

private void cbPlan_Click(object sender, EventArgs e)
      {
          cbPlan.Items.Clear();
          string pullsub = "select PlanID,PlanName from fbkPlanMaster(nolock)";
          string connString = ConfigurationManager.ConnectionStrings["Dbconn"].ToString();
          SqlConnection connection = new SqlConnection(connString); // defining sql connection
          SqlCommand cmd = new SqlCommand(pullsub, connection);
          cmd.CommandText = pullsub;
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          da.Fill(dt);
          cbPlan.ValueMember = "PlanID";
          cbPlan.DisplayMember = "PlanName";
          cbPlan.DataSource = dt;


      }


这篇关于显示计划名称但保留计划ID并在组合框中检索ID的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 05:10