在C#中,怎么绑定combox,要同时绑定valuesmember和displaymember。
我查找出来了课室类型名称和课室类型ID,其中课室类型名称是显示的值,课室类型ID是真实值。现在我想在combox中默认显示一个"全部"字段,用于查找全部的课室类型。我该怎...
我查找出来了课室类型名称和课室类型ID,其中课室类型名称是显示的值,课室类型ID是真实值。现在我想在combox中默认显示一个"全部"字段,用于查找全部的课室类型。我该怎么办?
展开
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace publicClass.publicClass.other
{
public class ComBDa
{
private string _StrValue;
private string _StrText;
public ComBDa(string StrValue, String StrText)
{
_StrText = StrText;
_StrValue = StrValue;
}
//覆盖该方法是为了使该对象的值在下拉列表框中显示出来
public override string ToString()
{
return StrText;
}
public string StrValue
{
get { return _StrValue; }
set { _StrValue = value; }
}
public string StrText
{
get { return _StrText; }
set { _StrText = value; }
}
//通过键值得到描述
public static string getText(ComboBox pCob, string pKey)
{
foreach (object obj in pCob.Items)
{
ComBDa sc = (ComBDa)obj;
if (sc._StrValue == pKey)
{
pCob.Text = sc._StrText;
return sc._StrText;
}
}
return "";
}
//通过描述得到键值
public static string getKey(ComboBox pCob, string pText)
{
string txt = pText;
if (txt == null || txt == "")
{
if (pCob.SelectedIndex > -1)
{
ComBDa sc = (ComBDa)pCob.Items[pCob.SelectedIndex];
return sc._StrValue;
}
}
foreach (object obj in pCob.Items)
{
ComBDa sc = (ComBDa)obj;
if (sc._StrText == txt) return sc._StrValue;
}
return txt;
}
//pCob选中值为pvalue的哪一项
public static int getSelectIndex(ComboBox pCob, string pvalue)
{
int j = 0;
for (int i = 0; i < pCob.Items.Count; i++)
{
ComBDa sc1 = (ComBDa)pCob.Items[i];
if (sc1.StrValue == pvalue)
{
j = i;
break;
}
}
return j;
}
//pCob选中文本为ptext的哪一项
public static int getSelectIndexText(ComboBox pCob, string ptext)
{
int j = 0;
for (int i = 0; i < pCob.Items.Count; i++)
{
ComBDa sc1 = (ComBDa)pCob.Items[i];
if (sc1.StrText == ptext)
{
j = i;
break;
}
}
return j;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace publicClass.publicClass.other
{
public class ComBDa
{
private string _StrValue;
private string _StrText;
public ComBDa(string StrValue, String StrText)
{
_StrText = StrText;
_StrValue = StrValue;
}
//覆盖该方法是为了使该对象的值在下拉列表框中显示出来
public override string ToString()
{
return StrText;
}
public string StrValue
{
get { return _StrValue; }
set { _StrValue = value; }
}
public string StrText
{
get { return _StrText; }
set { _StrText = value; }
}
//通过键值得到描述
public static string getText(ComboBox pCob, string pKey)
{
foreach (object obj in pCob.Items)
{
ComBDa sc = (ComBDa)obj;
if (sc._StrValue == pKey)
{
pCob.Text = sc._StrText;
return sc._StrText;
}
}
return "";
}
//通过描述得到键值
public static string getKey(ComboBox pCob, string pText)
{
string txt = pText;
if (txt == null || txt == "")
{
if (pCob.SelectedIndex > -1)
{
ComBDa sc = (ComBDa)pCob.Items[pCob.SelectedIndex];
return sc._StrValue;
}
}
foreach (object obj in pCob.Items)
{
ComBDa sc = (ComBDa)obj;
if (sc._StrText == txt) return sc._StrValue;
}
return txt;
}
//pCob选中值为pvalue的哪一项
public static int getSelectIndex(ComboBox pCob, string pvalue)
{
int j = 0;
for (int i = 0; i < pCob.Items.Count; i++)
{
ComBDa sc1 = (ComBDa)pCob.Items[i];
if (sc1.StrValue == pvalue)
{
j = i;
break;
}
}
return j;
}
//pCob选中文本为ptext的哪一项
public static int getSelectIndexText(ComboBox pCob, string ptext)
{
int j = 0;
for (int i = 0; i < pCob.Items.Count; i++)
{
ComBDa sc1 = (ComBDa)pCob.Items[i];
if (sc1.StrText == ptext)
{
j = i;
break;
}
}
return j;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询