简体   繁体   English

如何将ID和名称存储在datagridview combox列中

[英]how to store id and name in datagridview combox column

i am creating an windows form application.In the application created a form for employees attendance . 我正在创建一个Windows窗体应用程序。在该应用程序中, 为员工出勤创建了一个窗体

The form contains Datagridview and there are two columns one for employees(which is combobox column ) and another is checkbox columns(whether employee is present or absent). 该表单包含Datagridview ,有两列,一列用于雇员(即combobox列 ),另一列是复选框列(无论雇员在场还是不在场)。 columns i am creating programmatically. 我正在以编程方式创建的专栏。

i have a list of employee object (which contain employee name and ID). 我有一个员工对象列表 (其中包含员工姓名和ID)。

i want to store name and id of the employee in datagridview combobox column so that i can save attendance details using employee id(not based on employees name) so how can is store employees name and id(both) in datagrdiview combobox column . 我想将员工的姓名和ID存储在datagridview组合框列中,这样我就可以使用员工ID(不基于员工的名称)保存出勤详细信息,因此如何将员工的姓名和ID(两者)存储在datagrdiview组合框列中

i want to display employees name in combobox column,not employees id. 我想在组合框中显示员工姓名,而不是员工ID。

so please provide solution. 因此请提供解决方案。

One way that you can achieve this is by adding the object to the combobox items (Combobox.Items.Add(EmployeeObject)) and after that setting the DispayMember property of the control(ComboBox) to the name property(or member). 实现此目的的一种方法是将对象添加到组合框项(Combobox.Items.Add(EmployeeObject)),然后将控件(ComboBox)的DispayMember属性设置为name属性(或成员)。 The next example creates an list of 10 Johns with unique ids and gets the ID of current selected item. 下一个示例创建一个包含唯一ID的10个John的列表,并获取当前所选项目的ID。

  public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            ComboboxItem item = new ComboboxItem("John", i);
            comboBox1.Items.Add(item);



        }
        comboBox1.DisplayMember = "Name";
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboboxItem item = (ComboboxItem)comboBox1.SelectedItem;
        MessageBox.Show(item.ID.ToString());
    }

Also checkout http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx 还可以检出http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM