C#?winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率詳解
更新時間:2023年08月04日 08:38:13 作者:chentiebo
這篇文章主要給大家介紹了關(guān)于C#?winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率,Winform?ComboBox數(shù)據(jù)綁定是指將數(shù)據(jù)源中的數(shù)據(jù)與ComboBox控件進(jìn)行關(guān)聯(lián),需要的朋友可以參考下
一、ComboBox兩種數(shù)據(jù)綁定的方法
1.1、方法一、DataTable
//創(chuàng)建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數(shù)據(jù)綁定
this.comboBox1.DataSource = dataTable;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ID";1.2、方法二、List
//創(chuàng)建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數(shù)據(jù)綁定Lsit
this.comboBox2.DataSource = list;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "SID";
return list;
}二、比較兩種方法的效率
2.1、窗體設(shè)計

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
//創(chuàng)建計時
Stopwatch str1 = new Stopwatch();
//計時開始
str1.Start();
//運(yùn)行方法一
GetDate1();
// 計時停止
str1.Stop();
//控件label1顯示出總共花費(fèi)的時間(單位毫秒)
this.label1Time1.Text = str1.Elapsed.TotalMilliseconds.ToString();
//創(chuàng)建計時
Stopwatch str2 = new Stopwatch();
//計時開始
str2.Start();
//運(yùn)行方法二
GetDate2();
// 計時停止
str2.Stop();
//控件label1顯示出總共花費(fèi)的時間(單位毫秒)
this.label1Time2.Text = str2.Elapsed.TotalMilliseconds.ToString();
#endregion
}
#region ComboBox兩種數(shù)據(jù)綁定的方法
//方法一,dataTable
public DataTable GetDate1()
{
//創(chuàng)建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);
// 數(shù)據(jù)綁定
this.comboBox1.DataSource = dataTable;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "ID";
return dataTable;
}
// 方法二,list
//創(chuàng)建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消耗的時長要少

總結(jié)
到此這篇關(guān)于C# winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率的文章就介紹到這了,更多相關(guān)C# winform ComboBox數(shù)據(jù)綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#自定義繁體和簡體字庫實(shí)現(xiàn)中文繁體和簡體之間轉(zhuǎn)換的方法
這篇文章主要介紹了C#自定義繁體和簡體字庫實(shí)現(xiàn)中文繁體和簡體之間轉(zhuǎn)換的方法,通過自定義繁簡轉(zhuǎn)換字庫實(shí)現(xiàn)繁體與簡體轉(zhuǎn)換的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
C#替換Word文檔中的書簽內(nèi)容的詳細(xì)步驟
Word書簽不但可以幫助讀者快速跳轉(zhuǎn)到想要查看的位置,而且可以用做“占位符”,本文總結(jié)了一個將常見的Word文檔元素替換進(jìn)書簽的C#解決方案,希望對大家有所幫助2025-08-08
C#數(shù)值轉(zhuǎn)換-隱式數(shù)值轉(zhuǎn)換表參考
隱式轉(zhuǎn)換就是直接使用,比如可以把一個 byte 類型直接用在 int 上2013-04-04
C#使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類型數(shù)據(jù)
這篇文章介紹了C#使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類型數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

