zoukankan      html  css  js  c++  java
  • WinForm下ComboBox设定SelectedValue总结

    最近在项目中处理一个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 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();
    }

    完整代码:

    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; } }
    }
    }


  • 相关阅读:
    "无法在证书存储区中找到清单签名证书"的解决办法
    ASP.net从服务器端向客户端弹出alert对话框,但不使页面变成白板
    C#日期相关操作
    在Lucene.net实现自定义排序
    做技术二十多年 突然明白的道理
    .net中sql防止注入式攻击
    开发可统计单词个数的Android驱动程序(1)
    乐博Android客户端(新浪微博)1.01发布,欢迎各位童鞋试用
    百度面试题:求绝对值最小的数
    赶紧升级到Android 2.3.4,体验最新的Android技术
  • 原文地址:https://www.cnblogs.com/downmoon/p/1942220.html
Copyright © 2011-2022 走看看