• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#WinformComBox三种赋值方式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

https://www.cnblogs.com/ingstyle/p/4815303.html

 

 

第一种方法:

DataTable dt = new DataTable();

dt.Columns.Add( "name" );

dt.Columns.Add( "value" );

DataRow dr = dt.NewRow();

dr[0] = "活动" ;

dr[1] = "1" ;

dt.Rows.Add(dr); DataRow dr1 = dt.NewRow();

dr1[0] = "生活" ;

dr1[1] = "2" ;

dt.Rows.Add(dr1);

this .comboBox1.DataSource = dt;

this .comboBox1.DisplayMember = "name" ;

this .comboBox1.ValueMember = "value" ;

//调用方法:

//string _value = comboBox1.SelectedValue.ToString();

第二种:

//首先添加一个ComboBoxItem类

public class ComboBoxItem

{

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;

} }
// 赋值方法

ComboBoxItem newitem = new ComboBoxItem();

newitem.Text = "男" ;

newitem.Value = "1" ;

ComboBoxItem newitem1 = new ComboBoxItem();

newitem1.Text = "女" ;

newitem1.Value = "0" ;

com_sex.Items.Add(newitem);

com_sex.Items.Add(newitem1);

// 调用方法:
ComboBoxItem sex_item = (ComboBoxItem)com_sex.SelectedItem;

int com_sex_value = Convert.ToInt32(sex_item.Value);

string _Name = sex_item.Text;

第三种:

//首先添加一个SetCls类

public    class    SetCls

{

private    string    ID;

private    string    NAME;  

public    SetCls( string    pid, string    pName)

{
this .ID =pid;

this .NAME =pName;

}

public    string    pID

{

get return    ID;}

}

public    string    pName

{

get return    NAME;}

} }

// 赋值方法:(使用ArrayList 要先引用命名空间using System.Collections;)

ArrayList lists = new ArrayList();

lists .Add( new SetCls ( "1" , "活动" ));

lists .Add( new SetCls ( "2" , "生活" ));

this .COMBOX.DisplayMember = "pID" ;

this .COMBOX.ValueMember = "pName" ;

this .COMBOX.DataSource = lists;

 // 调用方法:

  string com_sex_value = COMBOX.SelectedValue.ToString();

我用DataSet填充的数据库中的内容(我这个是直接赋值,并不像上面三个添加值给ComBox)

DataSet ds_zubie = new DataSet();

da = new SqlDataAdapter(sql_zubie, PublicDB.DBzbw);

da.Fill(ds_zubie, "zubie" );

com_paidan.DataSource = ds_zubie.Tables[ "zubie" ].DefaultView;//绑定数据源

com_paidan.ValueMember = "zubie_id" ;//赋值Value

com_paidan.DisplayMember = "zubie_name" ;//赋值显示名称

//调用方法:

string com_zubie_id = com_paidan.SelectedValue.ToString();

第一种方法:

DataTable dt = new DataTable();

dt.Columns.Add( "name" );

dt.Columns.Add( "value" );

DataRow dr = dt.NewRow();

dr[0] = "活动" ;

dr[1] = "1" ;

dt.Rows.Add(dr); DataRow dr1 = dt.NewRow();

dr1[0] = "生活" ;

dr1[1] = "2" ;

dt.Rows.Add(dr1);

this .comboBox1.DataSource = dt;

this .comboBox1.DisplayMember = "name" ;

this .comboBox1.ValueMember = "value" ;

//调用方法:

//string _value = comboBox1.SelectedValue.ToString();

第二种:

//首先添加一个ComboBoxItem类

public class ComboBoxItem

{

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;

} }
// 赋值方法

ComboBoxItem newitem = new ComboBoxItem();

newitem.Text = "男" ;

newitem.Value = "1" ;

ComboBoxItem newitem1 = new ComboBoxItem();

newitem1.Text = "女" ;

newitem1.Value = "0" ;

com_sex.Items.Add(newitem);

com_sex.Items.Add(newitem1);

// 调用方法:
ComboBoxItem sex_item = (ComboBoxItem)com_sex.SelectedItem;

int com_sex_value = Convert.ToInt32(sex_item.Value);

string _Name = sex_item.Text;

第三种:

//首先添加一个SetCls类

public    class    SetCls

{

private    string    ID;

private    string    NAME;  

public    SetCls( string    pid, string    pName)

{
this .ID =pid;

this .NAME =pName;

}

public    string    pID

{

get return    ID;}

}

public    string    pName

{

get return    NAME;}

} }

// 赋值方法:(使用ArrayList 要先引用命名空间using System.Collections;)

ArrayList lists = new ArrayList();

lists .Add( new SetCls ( "1" , "活动" ));

lists .Add( new SetCls ( "2" , "生活" ));

this .COMBOX.DisplayMember = "pID" ;

this .COMBOX.ValueMember = "pName" ;

this .COMBOX.DataSource = lists;

 // 调用方法:

  string com_sex_value = COMBOX.SelectedValue.ToString();

我用DataSet填充的数据库中的内容(我这个是直接赋值,并不像上面三个添加值给ComBox)

DataSet ds_zubie = new DataSet();

da = new SqlDataAdapter(sql_zubie, PublicDB.DBzbw);

da.Fill(ds_zubie, "zubie" );

com_paidan.DataSource = ds_zubie.Tables[ "zubie" ].DefaultView;//绑定数据源

com_paidan.ValueMember = "zubie_id" ;//赋值Value

com_paidan.DisplayMember = "zubie_name" ;//赋值显示名称

//调用方法:

string com_zubie_id = com_paidan.SelectedValue.ToString();


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TDD个人实践体会(C#)四发布时间:2022-07-10
下一篇:
初步学习“C#枚举”发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap