如何为combox C#选择数据库列值? [英] How do I select database column value to the combox C#?

查看:124
本文介绍了如何为combox C#选择数据库列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,并填充了country.if我编辑datagridview行,它还将根据值设置国家/地区。但是combobox没有选择价值



以下是我的代码



我尝试过:



i have a combobox and that is populated with list of country.if i edit the datagridview row it will also set the country according to the value. but combobox did not select value

below is my code

What I have tried:

public long CountryID { get; set; }




private async void PopulateCountry(bool IsRequired)
       {
           if (IsRequired)
           {
               try
               {
                   var countrylist = await Shared.PopulateCountry();
                   if (countrylist.Any())
                   {
                       CmbCountry.ValueMember = "ID";
                       CmbCountry.DisplayMember = "Text";
                       CmbCountry.DataSource = countrylist;

                       if (_CountryID > 0)
                       {
                           CmbCountry.SelectedValue = _CountryID;
                       }
                   }


               }
               catch (Exception ex)
               {
                   

               }
           }

       }

推荐答案

您应该尽快学会使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候pected,你接近一个bug。



正如你已经被告知的那样,删除 try catch 来获取错误通知。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

As you already been told, remove the try catch to get notices of errors.


这篇关于如何为combox C#选择数据库列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆