又卡壳了,给ComboBox.SelectedValue付值 一波还未平熄,一波又来侵xi,深深太平洋底深深伤心ing-ing-ing~~~[WinForm] 与 [WebForm] 都遇到这个问题,给 [下拉框付列表控件] 与 [网格中的下拉列] 中付不存在的值,这四种情况均出错。网上找的答案都不合意。这就是我,一个有着八九年软件开发经验的糟糕程序员。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵,再“糟糕”也得反问题说清楚啊,老兄!当然出错,问题是你想要什么?要解决什么?如果你付的值不是Items中的就没有意义。 winform下赋值combobox.DisplayMember="字段名"//显示的值 combobox.ValueMember="字段名"//项的实际值webform下赋值 dropdownlist.DataTextField ="字段名"//显示的值 dropdownlist.DataValueField ="字段名"//项实际的值 dropdownlist FindByValue查询值是否存在 //WinForm.ComboBoxcmbDept.ValueMember = "Code";cmbDept.DisplayMember = "Name";cmbDept.DataSource = myBll.GetAllDepartment(); //[ITS, PUR, SAL, MGR, WHS, FIN]cmbDept.SelectedValue = "NOT_IN_DB"; //出错//WinForm.DataGridViewComboBoxColumnDataGridViewColumn col = new DataGridViewComboBoxColumn();col.DataSource = myBll.GetAllDepartment();col.ValueMember = "Code";col.DisplayMember = "Name";//在数据库后台把[员工部门]改为[部门表]中不存在的(特别是空字符串或那该S的NULL),显示网格就会出错 那还不出错啊,出错很正常。两种方式解决:第一个不赋值,只是指定text属性。第二个方法,try一下,出错不做任何处理。 网格绑定是没得try的,况且一个系统里边到处是try,try不tire呀?在这系统中付值,一定要用Key,不能用Text。 为什么一定要用Key,不存在的Key是肯定会出错的。或者在数据源中增加这样一项,只是在更新的时候注意去掉就是了。 cmbDept.SelectedValue = "NOT_IN_DB"; //出错这行例子是举得夸张了点,但在运行的时候难免会出现值为[空字符串、NULL]的情况,比如窗体初始化的时候。噹出个错误框会让用户觉得系统很不稳定。 定义类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 ComboBox.SelectedValue 是个杯具 老是Null 应该说M$产品是个杯俱,老是Null。一架飞机就一颗小螺丝为Null,结果呢?Destory() 应该说M$产品是个杯俱,老是Null。一架飞机就一颗小螺丝为Null,结果呢?Dispose() 输出字符串保存的值的值 VB.NET里面这个函数对应C#里面哪个函数 dat代替数据库存放数据的思路 求教:XSD验证float值为NaN值,居然不报错,即使minInclusive value为0? 医院管理系统B/S或C/S TextBox文字前加行号 用jquery+.net做三级联动出现的问题,第二个下拉框在特定情况下不触发onchange事件,求帮助 请教try ...catch的问题,为什么正常的语句也总是有异常? 思归大哥,帮忙啊,200 关于PetShop的问题 超级菜鸟问题···· 求助字符串比较问题
combobox.ValueMember="字段名"//项的实际值
webform下赋值
dropdownlist.DataTextField ="字段名"//显示的值
dropdownlist.DataValueField ="字段名"//项实际的值
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),显示网格就会出错
两种方式解决:
第一个不赋值,只是指定text属性。
第二个方法,try一下,出错不做任何处理。
在这系统中付值,一定要用Key,不能用Text。
或者
在数据源中增加这样一项,只是在更新的时候注意去掉就是了。
这行例子是举得夸张了点,但在运行的时候难免会出现值为[空字符串、NULL]的情况,比如窗体初始化的时候。
噹出个错误框会让用户觉得系统很不稳定。
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
一架飞机就一颗小螺丝为Null,结果呢?Destory()
一架飞机就一颗小螺丝为Null,结果呢?Dispose()