您的位置:首页 > 编程语言 > C#

在C#中使用组合框数据绑定的问题

2010-01-04 23:40 239 查看
在C#中使用数据绑定后例如:
   private void InitCombo()
        {
            this.cobRoomTypeName.DataSource = RoomTypeManager.GetAllRoomType();
            this.cobRoomTypeName.DisplayMember = "TypeName";
            this.cobRoomTypeName.ValueMember = "TypeID";
        }
绑定组合框。
那么当进行绑定的时候,会自动触发SelectedIndexChange事件。
那么如果定义如下方法:
        private void cobRoomTypeName_SelectedIndexChanged(object sender, EventArgs e)
       {
           if (this.cobRoomTypeName.SelectedValue != null)
           {
               int typeId = Convert.ToInt32(this.cobRoomTypeName.SelectedValue);
               RoomType rt = RoomTypeManager.GetRoomTypeByTypeId(typeId);
               this.txtPrice.Text = rt.TypePrice + "";
           }
        }
想要获取选择的Value属性,就会出错,应该如下处理:
        /// <summary>
        /// 使用这个事件可以在修改组合框内容的时候修改价格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cobRoomTypeName_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (this.cobRoomTypeName.SelectedValue != null)
            {
                int typeId = Convert.ToInt32(this.cobRoomTypeName.SelectedValue);
                RoomType rt = RoomTypeManager.GetRoomTypeByTypeId(typeId);
                this.txtPrice.Text = rt.TypePrice + "";
            }
        } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# object null