ComboBox/ListBox绑定数据
来源:互联网 发布:网络情人歌曲 编辑:程序博客网 时间:2024/05/21 10:50
在VS中经常使用ComboBox或ListBox控件的朋友都知道,绑定数据的时候,一般只能绑定Text值,而没有Value值,今天介绍如何将2个值绑定到ComboBox或ListBox控件中的某一项。
首先建立一个类:
using System;
using System.Collections.Generic;
using System.Text;
namespace List
{
public class cls_List
{
/// <summary>
/// List的项
/// </summary>
public class ListItem : System.Object
{
private string m_sValue = string.Empty;
private string m_sText = string.Empty;
/// <summary>
/// 值
/// </summary>
public string Value
{
get { return this.m_sValue; }
}
/// <summary>
/// 显示的文本
/// </summary>
public string Text
{
get { return this.m_sText; }
}
public ListItem(string value, string text)
{
this.m_sValue = value;
this.m_sText = text;
}
public override string ToString()
{
return this.m_sText;
}
public override bool Equals(System.Object obj)
{
if (this.GetType().Equals(obj.GetType()))
{
ListItem that = (ListItem)obj;
return (this.m_sText.Equals(that.Value));
}
return false;
}
public override int GetHashCode()
{
return this.m_sValue.GetHashCode(); ;
}
}
}
}
【调用方法】:
以ListBox控件为例,控件名称为:listBox1 ;ComboBox控件使用与ListBox基本一样
DataTable DsLogin = XXXXXXXXXXXX; //查询出的结果集
List<cls_List.ListItem> listItems = new List<cls_List.ListItem>();
for (int i = 0; i <DsLogin.Rows.Count; i++)
{
listItems.Add(new cls_List.ListItem(DsLogin.Rows[0][0].ToString(), DsLogin.Rows[0][1].ToString()));
}
listBox1.DataSource = listItems; //绑定数据
listBox1.DisplayMember = "Text"; //显示
listBox1.ValueMember = "Value"; //值
首先建立一个类:
using System;
using System.Collections.Generic;
using System.Text;
namespace List
{
public class cls_List
{
/// <summary>
/// List的项
/// </summary>
public class ListItem : System.Object
{
private string m_sValue = string.Empty;
private string m_sText = string.Empty;
/// <summary>
/// 值
/// </summary>
public string Value
{
get { return this.m_sValue; }
}
/// <summary>
/// 显示的文本
/// </summary>
public string Text
{
get { return this.m_sText; }
}
public ListItem(string value, string text)
{
this.m_sValue = value;
this.m_sText = text;
}
public override string ToString()
{
return this.m_sText;
}
public override bool Equals(System.Object obj)
{
if (this.GetType().Equals(obj.GetType()))
{
ListItem that = (ListItem)obj;
return (this.m_sText.Equals(that.Value));
}
return false;
}
public override int GetHashCode()
{
return this.m_sValue.GetHashCode(); ;
}
}
}
}
【调用方法】:
以ListBox控件为例,控件名称为:listBox1 ;ComboBox控件使用与ListBox基本一样
DataTable DsLogin = XXXXXXXXXXXX; //查询出的结果集
List<cls_List.ListItem> listItems = new List<cls_List.ListItem>();
for (int i = 0; i <DsLogin.Rows.Count; i++)
{
listItems.Add(new cls_List.ListItem(DsLogin.Rows[0][0].ToString(), DsLogin.Rows[0][1].ToString()));
}
listBox1.DataSource = listItems; //绑定数据
listBox1.DisplayMember = "Text"; //显示
listBox1.ValueMember = "Value"; //值
0 0
- ComboBox/ListBox绑定数据
- VB.NET数据源与ListBox,ComboBox,TextBox的数据绑定
- WPF listbox combobox简单绑定
- ListBox的数据绑定
- ListBox绑定数据
- WPF ListBox数据绑定
- 如何为winform中的列表控件Combobox、ListBox控件绑定数据
- C#初学踩坑日记-ListBox和ComboBox数据绑定同一个DataSet的问题
- c# combobox 数据绑定
- ComboBox 动态绑定数据
- ComboBox 数据绑定问题
- WPF 数据绑定 combobox
- ComboBox数据的绑定
- Combobox 控件绑定数据
- delphi绑定ComboBox数据
- listBox怎样实现数据绑定
- ListBox控件的数据绑定
- ListBox控件的数据绑定
- 【数据库连接方式】SqlParameter的用法
- ASP.NET页面传值方法
- VB 6.0 使用ADODB对象
- 在网页设计中给网页插入flash动画的方法
- Flash中AS的代码提示功能
- ComboBox/ListBox绑定数据
- 新概念第一册听力笔记
- 【转】什么是文案策划?文案策划怎么写?通通告诉你。。。
- 【转】软件项目策划过程(一)
- 【转】软件项目策划过程(二)
- 【转】软件项目策划过程(三)
- 【转】项目开发计划
- 【转】PhotoShop 修改人物学习
- .Net平台下开发中文语音应用程序