一波还未平熄,一波又来侵xi,深深太平洋底深深伤心ing-ing-ing~~~[WinForm] 与 [WebForm] 都遇到这个问题,给 [下拉框付列表控件] 与 [网格中的下拉列] 中付不存在的值,这四种情况均出错。
网上找的答案都不合意。
这就是我,一个有着八九年软件开发经验的糟糕程序员。

解决方案 »

  1.   

    呵呵,再“糟糕”也得反问题说清楚啊,老兄!当然出错,问题是你想要什么?要解决什么?如果你付的值不是Items中的就没有意义。
      

  2.   

    winform下赋值combobox.DisplayMember="字段名"//显示的值
                 combobox.ValueMember="字段名"//项的实际值
    webform下赋值
                   dropdownlist.DataTextField ="字段名"//显示的值
                   dropdownlist.DataValueField ="字段名"//项实际的值
      

  3.   

    dropdownlist FindByValue查询值是否存在
      

  4.   

    //WinForm.ComboBox
    cmbDept.ValueMember = "Code";
    cmbDept.DisplayMember = "Name";
    cmbDept.DataSource = myBll.GetAllDepartment(); //[ITS, PUR, SAL, MGR, WHS, FIN]cmbDept.SelectedValue = "NOT_IN_DB"; //出错//WinForm.DataGridViewComboBoxColumn
    DataGridViewColumn col = new DataGridViewComboBoxColumn();
    col.DataSource = myBll.GetAllDepartment();
    col.ValueMember = "Code";
    col.DisplayMember = "Name";
    //在数据库后台把[员工部门]改为[部门表]中不存在的(特别是空字符串或那该S的NULL),显示网格就会出错
      

  5.   

    那还不出错啊,出错很正常。
    两种方式解决:
    第一个不赋值,只是指定text属性。
    第二个方法,try一下,出错不做任何处理。
      

  6.   

    网格绑定是没得try的,况且一个系统里边到处是try,try不tire呀?
    在这系统中付值,一定要用Key,不能用Text。
      

  7.   

    为什么一定要用Key,不存在的Key是肯定会出错的。
    或者
    在数据源中增加这样一项,只是在更新的时候注意去掉就是了。
      

  8.   

    cmbDept.SelectedValue = "NOT_IN_DB"; //出错
    这行例子是举得夸张了点,但在运行的时候难免会出现值为[空字符串、NULL]的情况,比如窗体初始化的时候。
    噹出个错误框会让用户觉得系统很不稳定。
      

  9.   

    定义类ListItemusing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace HS.Function
    {
        /// <summary>
        /// 选择项类,用于ComboBox或者ListBox添加项
        /// </summary>
        public class ListItem
        {
            private string id = string.Empty;
            private string name = string.Empty;
            public ListItem(string sid, string sname)
            {
                id = sid;
                name = sname;
            }
            public override string ToString()
            {
                return this.name;
            }
            public string ID
            {
                get
                {
                    return this.id;
                }
                set
                {
                    this.id = value;
                }
            }
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
        }
    }调用方法给combox赋值cbb_Curriculum_name            DataTable mydt = B_TB_Curriculum.GetAllList().Tables[0];
                if (0 != mydt.Rows.Count)
                {
                    cbb_Curriculum_name.Items.Clear();
                    for (int i = 0; i < mydt.Rows.Count; i++)
                    {
                        cbb_Curriculum_name.Items.Add(new HS.Function.ListItem(mydt.Rows[i]["TB_Curriculum_ID"].ToString(), mydt.Rows[i]["TB_Curriculum_Name"].ToString()));
                    }
                    System.Windows.Forms.Application.DoEvents();
                    cbb_Curriculum_name.SelectedIndex = 0;
                }提值M_TB_School_Curriculum.TB_Curriculum_ID = Convert.ToInt32(((HS.Function.ListItem)cbb_Curriculum_name.SelectedItem).ID);  //课程ID
      

  10.   

    ComboBox.SelectedValue 是个杯具 老是Null
      

  11.   

    应该说M$产品是个杯俱,老是Null。
    一架飞机就一颗小螺丝为Null,结果呢?Destory()
      

  12.   

    应该说M$产品是个杯俱,老是Null。
    一架飞机就一颗小螺丝为Null,结果呢?Dispose()