一、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消耗的时长要少文章来源:https://www.toymoban.com/news/detail-516322.html
文章来源地址https://www.toymoban.com/news/detail-516322.html
到了这里,关于C# winform中ComboBox两种数据绑定的方法及其效率的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!