大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
最近在项目中处理一个winForm中设定ComboBox值,奇怪的是用一个对象匹配ComboBox的Datasource集合的一个对象时,设置SelectedValue时,竟然得到SelectedValue为null。
网上搜索一下,有说是如果用DateSet绑定时,SelectedValue得到的是null或DataRowView。
后来反复实践,发现可以用以下方法实现:
- private void SetValueByItem()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.SelectedItem = ac.FindAll(delegate(Area ar) { return ar.Area_ID == a.Area_ID; })[0];
- lbResult.Text = cbList.SelectedItem.GetType().ToString() + “:” + cbList.SelectedValue.ToString();
- }
private void SetValueByItem() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.SelectedItem = ac.FindAll(delegate(Area ar) { return ar.Area_ID == a.Area_ID; })[0]; lbResult.Text = cbList.SelectedItem.GetType().ToString() + “:” + cbList.SelectedValue.ToString(); }
其他两种设置方式:
- private void SetValueByValue()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.SelectedValue = a.Area_ID;
- lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString();
- }
private void SetValueByValue() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.SelectedValue = a.Area_ID; lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString(); }
- private void SetValueByText()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.SelectedIndex = cbList.FindString(a.Area_Name);
- lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString();
- }
private void SetValueByText() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.SelectedIndex = cbList.FindString(a.Area_Name); lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString(); }
完整代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace WinFormCombobox
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
- #region Members
- AreaLists ac;
- public AreaLists GetTestArea()
- {
- AreaLists al = new AreaLists();
- for (int i = 1; i < 11; i++)
- {
- Area a = new Area();
- a.Area_ID = i.ToString();
- a.Area_Name = “第” + i.ToString() + “名”;
- al.Add(a);
- }
- return al;
- }
- #endregion
- #region Events
- private void frmMain_Load(object sender, EventArgs e)
- {
- ac = GetTestArea();
- cbList.DataSource = ac;
- cbList.DisplayMember = “Area_Name”;
- cbList.ValueMember = “Area_ID”;
- }
- private void btnSetCombobox_Click(object sender, EventArgs e)
- {
- SetValue0();
- //SetValueByValue();
- SetValueByItem();
- SetValueByText();
- }
- #endregion
- #region Methods
- private void SetValue0()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.DataSource = GetTestArea();
- cbList.DisplayMember = “Area_Name”;
- cbList.ValueMember = “Area_ID”;
- cbList.SelectedValue = a.Area_ID;
- lbResult.Text = cbList.SelectedValue.ToString();
- }
- private void SetValueByValue()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.SelectedValue = a.Area_ID;
- lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString();
- }
- private void SetValueByItem()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.SelectedItem = ac.FindAll(delegate(Area ar) { return ar.Area_ID == a.Area_ID; })[0];
- lbResult.Text = cbList.SelectedItem.GetType().ToString() + “:” + cbList.SelectedValue.ToString();
- }
- private void SetValueByText()
- {
- Area a = new Area();
- a.Area_ID = “8”;
- a.Area_Name = “第8名”;
- cbList.SelectedIndex = cbList.FindString(a.Area_Name);
- lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString();
- }
- #endregion
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace WinFormCombobox
- {
- #region Area
- public class Area
- {
- private string m_Area_ID;
- public string Area_ID
- {
- get { return m_Area_ID; }
- set { m_Area_ID = value; }
- }
- private string m_Area_Name;
- public string Area_Name
- {
- get { return m_Area_Name; }
- set { m_Area_Name = value; }
- }
- private double m_Area_Order;
- public double Area_Order
- {
- get { return m_Area_Order; }
- set { m_Area_Order = value; }
- }
- }
- #endregion
- [Serializable]
- public class AreaLists : List<Area>
- {
- private int _maxItems = 0;
- public int MaxItems { get { return this._maxItems; } set { this._maxItems = value; } }
- }
- }
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WinFormCombobox{ public partial class frmMain : Form { public frmMain() { InitializeComponent(); } #region Members AreaLists ac; public AreaLists GetTestArea() { AreaLists al = new AreaLists(); for (int i = 1; i < 11; i++) { Area a = new Area(); a.Area_ID = i.ToString(); a.Area_Name = “第” + i.ToString() + “名”; al.Add(a); } return al; } #endregion #region Events private void frmMain_Load(object sender, EventArgs e) { ac = GetTestArea(); cbList.DataSource = ac; cbList.DisplayMember = “Area_Name”; cbList.ValueMember = “Area_ID”; } private void btnSetCombobox_Click(object sender, EventArgs e) { SetValue0(); //SetValueByValue(); SetValueByItem(); SetValueByText(); } #endregion #region Methods private void SetValue0() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.DataSource = GetTestArea(); cbList.DisplayMember = “Area_Name”; cbList.ValueMember = “Area_ID”; cbList.SelectedValue = a.Area_ID; lbResult.Text = cbList.SelectedValue.ToString(); } private void SetValueByValue() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.SelectedValue = a.Area_ID; lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString(); } private void SetValueByItem() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.SelectedItem = ac.FindAll(delegate(Area ar) { return ar.Area_ID == a.Area_ID; })[0]; lbResult.Text = cbList.SelectedItem.GetType().ToString() + “:” + cbList.SelectedValue.ToString(); } private void SetValueByText() { Area a = new Area(); a.Area_ID = “8”; a.Area_Name = “第8名”; cbList.SelectedIndex = cbList.FindString(a.Area_Name); lbResult.Text = cbList.SelectedValue.GetType().ToString() + “:” + cbList.SelectedValue.ToString(); } #endregion }}using System;using System.Collections.Generic;using System.Text;namespace WinFormCombobox{ #region Area public class Area { private string m_Area_ID; public string Area_ID { get { return m_Area_ID; } set { m_Area_ID = value; } } private string m_Area_Name; public string Area_Name { get { return m_Area_Name; } set { m_Area_Name = value; } } private double m_Area_Order; public double Area_Order { get { return m_Area_Order; } set { m_Area_Order = value; } } } #endregion [Serializable] public class AreaLists : List<Area> { private int _maxItems = 0; public int MaxItems { get { return this._maxItems; } set { this._maxItems = value; } } }}
邀月注:本文版权由邀月和CSDN共同所有,转载请注明出处。
助人等于自助! 3w@live.cn
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/207128.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...