首页 > 编程语言 >c# ComboBox控件的常用一些属性和用法、事件及数据绑定方法

c# ComboBox控件的常用一些属性和用法、事件及数据绑定方法

时间:2024-01-30 18:35:11浏览次数:23  
标签:控件 ShowValue c# ComboBox 绑定 Add dt comboBox1 ID

一、常用属性和用法

1、Text:获取或设置与此控件关联的文本。

//设置默认值
this.comboBox1.Text = "请选择内容";
//or
comboBox1.Items.Add("请选择内容");
comboBox1.SelectedIndex = 0;

2、SelectedIndex:获取或设置指定当前选定项的索引。(设置新索引会 SelectedIndexChanged 引发 事件。)

3、SelectedItem:获取或设置 ComboBox 中当前选定的项。

//取得当前选择的Item的显示值
this.comboBox1.SelectedItem.ToString();

4、SelectedText:获取或设置 ComboBox 的可编辑部分中选定的文本。如果 DropDownStyle 设置为 DropDownList,则返回值为空字符串 ("")。

5、数据绑定:

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "dtColName";
comboBox1.ValueMember = "ID";
/*DisplayMember绑定的是需显示的字段,ValueMember绑定的是对应的值
一般DisplayMember是显示给客户看的, 而ValueMember 是绑定处理程序标识 给程序员看的*/

combobox控件显示的是DisplayMember 绑定的字段,也就是说comboBox1的text属性取的值是DisplayMember 绑定的字段。

6、获取ValueMember的值:

DataRowView drv = (DataRowView)comboBox1.SelectedItem;
ID= Convert.ToInt32(drv.Row["dtColName2"].ToString());

7、SelectedValue:获取或设置由 ValueMember 属性指定的成员属性的值。 类型:System.Object,包含由ValueMember 属性指定的数据源成员的值的对象。
当设置了DataSource 属性后,SelectedValue 属性值会默认为第一行,因此,如果不希望ComboBox自动选择第一行,还需在设置完DataSource 后自行将SelectedValue 设为“”。(根据SelectedValue的值类型设置,如果是字符就设置“”,如果是数值可设置为0)



二、数据绑定方法

1、绑定List集合:

private void ListDataBinding()
{
   List<string>list = new List<string>()
   {
      "test1","test2","test3"
   };
   this.comboBox1.DataSource = list;
}

2、绑定数组集合:

private void ArrayDataBinding()
{
   string[] array = new string[] { "test1","test2","test3" };
   this.comboBox1.DataSource = array;
}

3、绑定数组集合:

//创建一个实体类,用于存储数据
public class Data
{
   public string ID { get; set; }
   public string ShowValue { get; set; }
}

List<Data> datas = new List<Data>();
private void ArrayDataBinding()
{
            
   Data data1 = new Data() { ID = "1", ShowValue = "test1" };
   Data data2 = new Data() { ID = "2", ShowValue = "test2" };
   Data data3 = new Data() { ID = "3", ShowValue = "test3" };
   datas.Add(data1);
   datas.Add(data2);
   datas.Add(data3);
 
   this.comboBox1.DataSource = datas;
   this.comboBox1.DisplayMember = "ShowValue";
   this.comboBox1.ValueMember = "ID";
}

//利用控件的SelectedIndexChanged事件选中的DisplayMember来查找对应的ValueMember。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   this.textBox1.Text = comboBox1.SelectedValue.ToString();
}

结果如图:

4、绑定DataTable:

private void DataTableDataBinding()
{
    DataTable dt = new DataTable();
    DataColumn dc1 = new DataColumn("ID");
    DataColumn dc2 = new DataColumn("ShowValue");
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
 
    DataRow dr1 = dt.NewRow();
    dr1["ID"] = "1";
    dr1["ShowValue"] = "test1";
    DataRow dr2 = dt.NewRow();
    dr2["ID"] = "2";
    dr2["ShowValue"] = "test2";
    DataRow dr3 = dt.NewRow();
    dr3["ID"] = "3";
    dr3["ShowValue"] = "test3";
 
    dt.Rows.Add(dr1);
    dt.Rows.Add(dr2);
    dt.Rows.Add(dr3);
 
    this.comboBox1.DataSource = dt;
    this.comboBox1.ValueMember = "ID";
    this.comboBox1.DisplayMember = "ShowValue";
}

//利用控件的SelectedIndexChanged事件选中的DisplayMember来查找对应的ValueMember。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.textBox1.Text = comboBox1.SelectedValue.ToString();
}

结果如图:

5、绑定枚举:

//定义一个颜色枚举
public enum ColorEnum
{
   red,
   blue,
   skyblue
}

//使用Enum.GetNames()方法,将枚举元素的名称赋值给控件。
private void EnumDataBinding()
{
   this.comboBox1.DataSource = Enum.GetNames(typeof(ColorEnum));
}

6、利用Items.Add方法添加元素:

private void ItemsDataBinding()
{
   this.comboBox1.Items.Add("test1");
   this.comboBox1.Items.Add("test2");
   this.comboBox1.Items.Add("test3");
}

7、Items.AddRange:

private void AddRangeDataBinding()
{
   object[] array = new object[] { test1, test2, test3 };
   this.comboBox1.Items.AddRange(array);            
}

Items.AddRange()方法里面的参数为Object类型的数组。

标签:控件,ShowValue,c#,ComboBox,绑定,Add,dt,comboBox1,ID
From: https://www.cnblogs.com/sumu80/p/17996710

相关文章

  • epoll_create函数
    目录函数简介低层实现逻辑epoll_create与epoll_create1函数的区别函数简介/*Createsanepollinstance.Returnsanfdforthenewinstance.The"size"parameterisahintspecifyingthenumberoffiledescriptorstobeassociatedwiththenewinstance.......
  • npm编译vue出错:Error code CERT_HAS_EXPIRED
    [Error]Theerrormessageisabouttheregistryhttps://npm.sap.com/youused.npmERR!codeCERT_HAS_EXPIREDnpmERR!errnoCERT_HAS_EXPIREDnpmERR!requesttohttps://npm.sap.com/@sap%2fcdsfailed,reason:certificatehasexpired[Solution]runcommand......
  • P1699 [USACO19OPEN] Bucket Brigade B
    题目大意给一个\(10×10\)字符串矩阵,求从\(L\)开始(不经过\(R\))到\(B\)的短路径。思路这道题因为是求最短,所以用\(DFS\)比较麻烦,于是我用的是\(BFS\)做。遇到障碍则跳过,到终点直接退出就行了。code#include<iostream>usingnamespacestd;structnode{intx,y......
  • PGC、UGC、AIGC发展
    AIGC(ArtificialIntelligenceGeneratedContent人工智能生成内容)时代已经来临并疯狂发展,迅速催生了全新的技术革命。本文主要介绍web1.0到如今的web3.0和PGC到UGC再到AIGC。互联网形态web1.0web2.0web3.0内容生产方式PGC(专业生产)UGC(专业生产)AIGC(AI生产)生产主......
  • Ali Cloud Linux3 : Installing ImageMagick for PHP 7.4
    Step1:Ifnotalreadyinstalled,installthephp-develandgccpackages.$sudoyuminstallphp-develgccTocheckifthepackagesareinstalled,usethefollowingcommands.Ifeithercommandreturnsanemptyresult(orbothdo),you’llneedtoinstallth......
  • CF1904C Array Game
    题目传送门codeforces洛谷题目大意给你一个由\(n\)个正整数组成的数组\(a\)。在一次操作中,选取\((i,j)\),将\(|a_i-a_j|\)加到\(a\)的末尾。你的任务是在执行\(k\)操作后,最小化最后数组\(a\)的最小值。思路分三种情况:\(k\geq3\)时,我们可以取两次相同......
  • Qt error C1083:无法打开文件stddef.h或crtdbg.h
    问题描述环境:QT5.15.2从别的电脑拷过来一个能跑的项目在新安装的qt上运行,报错C1038,检查发现报错的文件都跟sdk有关,问题就是不能正确找到SDK相关的。解决1.查找SDK(我是用的"everything"工具搜索的,一般都会在这个路径下面) 在这几个文件夹中选择最新的那个,点进去,里边......
  • Qt 使用MSVC2017编译报错: C1083:无法打开包括文件: “stddef.h“的解决方案
    之前安装过QT的好几个版本:5.9,5.12,5.15,编译过项目。现在使用QT5.12.6+MSVC2017编译项目出现如下图所示报错,困扰了我2天。一开始,我通过卸载重装QT和 VS2017 都没有解决问题。今天晚上找到一个办法,就是在QT“项目”设置里面将头文件目录配置进去,终于将问题解决......
  • CSharp: iText 8.0 in donet 4.8.1
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.IO;usingSystem.Text;usingiText.IO.Font;usingiText.IO.Image;usingiText.Kernel.Font;usingiTe......
  • C#double类型转换成科学计数法类型(全网最全最好回答)
     doubledoubleData=heatData.MaxSampleValue.ToString("0.0000E+0"); 众所周知G7的转换是有精度限制的,所以:doublevalue1=1234.56789;doublevalue2=0.000123456789;doublevalue3=12345678901234567890.123456789;stringf......