zoukankan      html  css  js  c++  java
  • C# ComboBox绑定值问题

    使用这种方式始终绑定值有问题:

    cbxSchool.DataSource = schoolList;
    cbxSchool.DisplayMember = "school_name";
    cbxSchool.ValueMember = "school_id";

    选择改变事件获取选中值:cbxSchool.SelectedValue 始终是对象,不是想要的id。

    解决方法:

    if (schoolList != null && schoolList.Count > 0)
    {
    cbxSchool.Items.Clear();
    for (int i = 0; i < schoolList.Count; i++)
    {
    cbxSchool.Items.Add(schoolList[i].school_name);
    }

    //选择默认值

    int selectIndex = schoolList.FindIndex(a => a.school_id == schoolId);
    cbxSchool.SelectedIndex = selectIndex == -1 ? 0 : selectIndex;

    //获取选中值

      string  schoolName = schoolList[cbxSchool.SelectedIndex].school_name;

    }

  • 相关阅读:
    12.1
    11.26
    12.5Java日报
    11.25
    11.27
    12.03
    11.28
    12.04
    如何在TortoiseGit中使用sshkeygen生成的key
    leetcode 39 组合总和
  • 原文地址:https://www.cnblogs.com/YoungHeart/p/10475348.html
Copyright © 2011-2022 走看看