ComboBox显示成员选择 [英] ComboBox displaymember select

查看:73
本文介绍了ComboBox显示成员选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



在我的应用程序中,我有一个从数据库填充的组合框:



Hello,

In my application i have a combobox populated from database:

Program.Connection.CommandText = "SELECT LastName + ', ' + FirstName + ' ' + IIF(MiddleName, MiddleName,'') AS NumeComplet, ClientId FROM Clients GROUP BY ClientId, LastName, FirstName, MiddleName ORDER BY ClientId";
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            cboNumeClient.DataSource = Table;
            cboNumeClient.DisplayMember = "NumeComplet";
            cboNumeClient.ValueMember = "ClientId";
            cboNumeClient.Focus();
            NumeClient();
            ClientiAdaugati = true;
            InformatiiClient();





和选定的索引事件:





and a selected index event:

private void cboNumeClient_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (this.ClientiAdaugati)
           {
               NumeClient();
               InformatiiClient();
           }
       }







在数据库中我有同样的客户名字和姓氏,每个人都有自己的ID。



当我选择i客户端时,我使用ID来填充数据网格视图。



问题是:



例如:

客户John John - ID - 2345,和

客户John John - ID - 2390.



当我从组合框中选择第二个名字(id - 2390)时,我的datagridview根据数据填充whit,但是当我从我的窗口点击一个控件时,我的组合框自动选择第一个客户端(ID 2345)。



我如何解决这个问题?




In the database i have customers with the same first name and last name, each one have it''s own ID.

When i select i client i use the ID to populate a datagridview.

The problem is:

For example the:
client John John - ID - 2345, and
client John John - ID - 2390.

When i select the second name (id - 2390) from combobox, my datagridview fill whit according data, but when i click on a control from my windows, my combobox auto select the firs client (id 2345).

How do i resolve that?

推荐答案

首先,没有这样的事件, cboNumeClient_SelectedIndexChanged 。它可以是你 handler ,它被添加到某个组合框的事件实例 SelectedIndexChanged 的调用列表中,但这在你的代码中并不明显。您还应该通过''+ =''运算符显示添加句柄的位置。因为你没有抱怨组合框选择没有任何反应,我认为处理程序是正确添加的。



现在,无论你有什么差异,它都是不明显你的处理程序代码是正确的,至少,它不是以有条不紊的方式编写的。您应该执行以下操作:
To start with, there is no such event, cboNumeClient_SelectedIndexChanged. It can be you handler which is added to an invocation list of the event instance SelectedIndexChanged of some combo box, but this is not apparent from your code. You also should show where the handle is added via the ''+='' operator. As you did not complain that nothing happens on combo box selection, I assume the handler is added properly.

Now, no matter what discrepancy in ID you have, it is not apparent that your handler code does right thing, at least, it is not written in a methodically right way. You should do something like this:
CboNumeClientSelectedIndexChangedHandler(object sender, EventArgs e) {
    ComboBox comboBox = (ComboBox)sender;
    // extract current selected index from combo box
    // or extract ID or any other information from selected item
    // use it
    // ...
}





请注意,我还重命名了您的处理程序,以避免违反Microsoft命名约定总是由自动生成的代码引起的。是的,您应该重命名所有自动生成的名称,但我刚才解释的原因,以及给出所有语义名称。



现在,即使这不是最好的方法。使用组合框时,其列表元素可以是任何对象。我认为你只使用字符串。这很糟糕,因为那时你必须从字符串中提取一些数据,这至少不利于维护,并且可能不可靠。文明方法是这样的:您创建一些数据类型,类或结构,包含您真正需要的所有信息。唯一的问题是:每个元素在UI中显示什么字符串?答案是:无论 ToString 返回。因此,您还需要为表示组合框列表项的struct / class重写 ToString 。相同的技术可用于列表框等。



-SA



Note that I also renamed you handler, to avoid the violation of Microsoft naming conventions always caused by that auto-generated code. Yes, you are supposed to rename all auto-generated names, but the reason I just explained, and also to give everything semantic names.

Now, even that is not a best approach. When you use a combo box, its list elements could be any objects. I think you are using just strings. This is bad, because then you have to extract some data from a string, which is at least bad for maintenance, and can be unreliable. The civilized approach is this: you create some data type, class or struct with all information you really need for your functionality. The only problem will be: what string will be shown in UI for each element? The answer is: whatever ToString returns. So, you also need to override ToString for your struct/class representing a combo box list item. Same technique can be used for list boxes, etc.

—SA


这篇关于ComboBox显示成员选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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