C# winform中ComboBox数据绑定的两种方法及效率详解
作者:chentiebo
这篇文章主要给大家介绍了关于C# winform中ComboBox数据绑定的两种方法及效率,Winform ComboBox数据绑定是指将数据源中的数据与ComboBox控件进行关联,需要的朋友可以参考下
一、ComboBox两种数据绑定的方法
1.1、方法一、DataTable
//创建DataTable DataTable dataTable = new DataTable(); dataTable.Columns.Add("ID"); dataTable.Columns.Add("Name"); DataRow dataRow = dataTable.NewRow(); dataRow["ID"] = "1"; dataRow["Name"] = "方法1-测试1"; dataTable.Rows.Add(dataRow); DataRow dataRow1 = dataTable.NewRow(); dataRow1["ID"] = "2"; dataRow1["Name"] = "方法1-测试2"; dataTable.Rows.Add(dataRow1); DataRow dataRow2 = dataTable.NewRow(); dataRow2["ID"] = "3"; dataRow2["Name"] = "方法1-测试3"; dataTable.Rows.Add(dataRow2); // ComboBox数据绑定 this.comboBox1.DataSource = dataTable; comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID";
1.2、方法二、List
//创建Test类 public class Test { public Test() { } public Test(int sid, string name, int age) { SID = sid; Name = name; Age = age; } public string Name { get; } public int Age { get; } public int SID { get; } } public List<Test> GetDate2() { List<Test> list = new List<Test>(); Test test = new Test(1, "方法二--测试1", 21); list.Add(test); Test test1 = new Test(2, "方法二--测试2", 22); list.Add(test1); Test test2 = new Test(3, "方法二--测试3", 23); list.Add(test2); // ComboBox数据绑定Lsit this.comboBox2.DataSource = list; comboBox2.DisplayMember = "Name"; comboBox2.ValueMember = "SID"; return list; }
二、比较两种方法的效率
2.1、窗体设计
2.2、代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); 方法一 //this.comboBox1.DataSource = GteDate1(); //comboBox1.DisplayMember = "Name"; //comboBox1.ValueMember = "ID"; 方法二 //this.comboBox2.DataSource = GetDate2(); //comboBox2.DisplayMember = "Name"; //comboBox2.ValueMember = "SID"; #region 处理2 //创建计时 Stopwatch str1 = new Stopwatch(); //计时开始 str1.Start(); //运行方法一 GetDate1(); // 计时停止 str1.Stop(); //控件label1显示出总共花费的时间(单位毫秒) this.label1Time1.Text = str1.Elapsed.TotalMilliseconds.ToString(); //创建计时 Stopwatch str2 = new Stopwatch(); //计时开始 str2.Start(); //运行方法二 GetDate2(); // 计时停止 str2.Stop(); //控件label1显示出总共花费的时间(单位毫秒) this.label1Time2.Text = str2.Elapsed.TotalMilliseconds.ToString(); #endregion } #region ComboBox两种数据绑定的方法 //方法一,dataTable public DataTable GetDate1() { //创建DataTable DataTable dataTable = new DataTable(); dataTable.Columns.Add("ID"); dataTable.Columns.Add("Name"); DataRow dataRow = dataTable.NewRow(); dataRow["ID"] = "1"; dataRow["Name"] = "方法1-测试1"; dataTable.Rows.Add(dataRow); DataRow dataRow1 = dataTable.NewRow(); dataRow1["ID"] = "2"; dataRow1["Name"] = "方法1-测试2"; dataTable.Rows.Add(dataRow1); DataRow dataRow2 = dataTable.NewRow(); dataRow2["ID"] = "3"; dataRow2["Name"] = "方法1-测试3"; dataTable.Rows.Add(dataRow2); // 数据绑定 this.comboBox1.DataSource = dataTable; comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "ID"; return dataTable; } // 方法二,list //创建Test类 public class Test { public Test() { } public Test(int sid, string name, int age) { SID = sid; Name = name; Age = age; } public string Name { get; } public int Age { get; } public int SID { get; } } public List<Test> GetDate2() { List<Test> list = new List<Test>(); Test test = new Test(1, "方法二--测试1", 21); list.Add(test); Test test1 = new Test(2, "方法二--测试2", 22); list.Add(test1); Test test2 = new Test(3, "方法二--测试3", 23); list.Add(test2); this.comboBox2.DataSource = list; comboBox2.DisplayMember = "Name"; comboBox2.ValueMember = "SID"; return list; } #endregion } }
2.3、两种方法消耗时间对比
list相对于DataTable消耗的时长要少
总结
到此这篇关于C# winform中ComboBox数据绑定的两种方法及效率的文章就介绍到这了,更多相关C# winform ComboBox数据绑定内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!