看代碼:public class ComboBoxItem
{
//為ComboBox添加value和text值
private string _text=null;
private object _value=null;
public string Text{get{return this._text;} set{this._text=value;}}
public object Value{get {return this._value;}
set{this._value=value;}}
public override string ToString()
{
return this._text;
}
}private void addComboBox()
{
DataView dataView = new DataView();
dataView = Agent_DataLayer.DsPositionAndStaff.StaffAndPosition.Copy().DefaultView;
dataView.RowFilter = "MarkId <> 0";
foreach(DataRowView drv in dataView)
{
int strVal = int.Parse(drv["staffId"].ToString());
string strText = drv["ItemFull"].ToString().Trim();
//(看下面一段,問題就在這裡,程序運行後,依然出現重復的strText)
if(!this.cmbStaffInfo.Items.Contains(strText))//過濾重復 {
ComboBoxItem newItem = new ComboBoxItem();
newItem.Text = strText; //添加text值
newItem.Value = strVal; //添加value值
this.cmbStaffInfo.Items.Add(newItem);
}
}
}
{
//為ComboBox添加value和text值
private string _text=null;
private object _value=null;
public string Text{get{return this._text;} set{this._text=value;}}
public object Value{get {return this._value;}
set{this._value=value;}}
public override string ToString()
{
return this._text;
}
}private void addComboBox()
{
DataView dataView = new DataView();
dataView = Agent_DataLayer.DsPositionAndStaff.StaffAndPosition.Copy().DefaultView;
dataView.RowFilter = "MarkId <> 0";
foreach(DataRowView drv in dataView)
{
int strVal = int.Parse(drv["staffId"].ToString());
string strText = drv["ItemFull"].ToString().Trim();
//(看下面一段,問題就在這裡,程序運行後,依然出現重復的strText)
if(!this.cmbStaffInfo.Items.Contains(strText))//過濾重復 {
ComboBoxItem newItem = new ComboBoxItem();
newItem.Text = strText; //添加text值
newItem.Value = strVal; //添加value值
this.cmbStaffInfo.Items.Add(newItem);
}
}
}
public class ComboBoxItem
{
//為ComboBox添加value和text值
private string _text = null;
private object _value = null;
public string Text { get { return this._text; } set { this._text = value; } }
public object Value
{
get { return this._value; }
set { this._value = value; }
}
public override string ToString()
{
return this._text;
}
public override bool Equals(object obj)
{
ComboBoxItem item = obj as ComboBoxItem;
if (item != null)
{
return object.Equals(this._value, item._value);
}
/*或
if (item!=null)
{
return this._text==item._text;
}
*/
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
{
DataView dataView = new DataView();
dataView = Agent_DataLayer.DsPositionAndStaff.StaffAndPosition.Copy().DefaultView;
dataView.RowFilter = "MarkId <> 0";
//用个辅助列表变通一下,:)
List<string> ItemTextList = new List<string>();
foreach(DataRowView drv in dataView)
{
int strVal = int.Parse(drv["staffId"].ToString());
string strText = drv["ItemFull"].ToString().Trim();
if (ItemTextList.Contains(strText) == false)
{
ItemTextList.Add(strText);
ComboBoxItem newItem = new ComboBoxItem();
newItem.Text = strText; //添加text值
newItem.Value = strVal; //添加value值
this.cmbStaffInfo.Items.Add(newItem);
}
}
}
private void addComboBox()
{
DataView dataView = new DataView();
dataView = Agent_DataLayer.DsPositionAndStaff.StaffAndPosition.Copy().DefaultView;
dataView.RowFilter = "MarkId <> 0";
foreach(DataRowView drv in dataView)
{
int strVal = int.Parse(drv["staffId"].ToString());
string strText = drv["ItemFull"].ToString().Trim();
ComboBoxItem newItem = new ComboBoxItem();
newItem.Text = strText; //添加text值
newItem.Value = strVal; //添加value值
if (cmbStaffInfo.Items.Contains(newItem) == false)
cmbStaffInfo.Items.Add(newItem);
}
}
要正确的返回结果就是重写Equals,因为你要按String来查,所以按你的情况,这个Equals应是这样的:
public override bool Equals(object obj)
{
if (obj is string && (string)text= this._text)
{
return true;
}
return false;
}