2007年12月26日星期三

ComboBox 手动绑定数据项


===========ValueObject.cs============

using System;
using System.Collections.Generic;
using System.Text;

namespace psms.util
{
    class ValueObject
    {
        private string value;
        private string text;

        public ValueObject(string v, string t)
        {
            this.value = v;
            this.text = t;
        }

        public string Value
        {
            get { return this.value;}
        }
        public string Text
        {
            get { return this.text; }
        }

    }
}

===============绑定数据===============

this.combIn_Acc.Items.Add(new util.ValueObject("", ""));
this.combIn_Acc.Items.Add(new util.ValueObject("0","未做帐"));
this.combIn_Acc.Items.Add(new util.ValueObject("1", "已做帐"));
           
this.combIn_Acc.ValueMember = "value";
this.combIn_Acc.DisplayMember = "text";
this.combIn_Acc.SelectedIndex = 0;


================取值==================


if (this.combIn_Acc.SelectedIndex >= 0)
{
   if (((util.ValueObject)this.combIn_Acc.SelectedItem).Value.ToString() != "")
   {
       condition.Append(" and in_acc = ").Append(((util.ValueObject)this.combIn_Acc.SelectedItem).Value.ToString()).Append(" ");
   }
}


不知道为什么this.combIn_Acc.SelectedValue总是报空指针异常,想不明白,换了上面的取值方法终于可行了

没有评论:

发表评论