C#?winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率詳解
更新時間:2023年08月04日 08:38:13 作者:chentiebo
這篇文章主要給大家介紹了關于C#?winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率,Winform?ComboBox數(shù)據(jù)綁定是指將數(shù)據(jù)源中的數(shù)據(jù)與ComboBox控件進行關聯(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、窗體設計

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();
//運行方法一
GetDate1();
// 計時停止
str1.Stop();
//控件label1顯示出總共花費的時間(單位毫秒)
this.label1Time1.Text = str1.Elapsed.TotalMilliseconds.ToString();
//創(chuàng)建計時
Stopwatch str2 = new Stopwatch();
//計時開始
str2.Start();
//運行方法二
GetDate2();
// 計時停止
str2.Stop();
//控件label1顯示出總共花費的時間(單位毫秒)
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消耗的時長要少

總結
到此這篇關于C# winform中ComboBox數(shù)據(jù)綁定的兩種方法及效率的文章就介紹到這了,更多相關C# winform ComboBox數(shù)據(jù)綁定內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# 實現(xiàn)基于ADO.NET框架的DBHelper工具類(簡化數(shù)據(jù)庫操作)
ADO.NET是.NET框架中用于與數(shù)據(jù)庫交互的核心組件,提供了一套用于連接數(shù)據(jù)庫、執(zhí)行SQL查詢、插入、更新和刪除數(shù)據(jù)的類庫,包括SqlConnection、SqlCommand、SqlDataReader等,本文介紹如何使用DBHelper類封裝數(shù)據(jù)庫操作,以提高代碼的可維護性和復用性,感興趣的朋友一起看看吧2024-08-08
C#通過配置文件動態(tài)修改web.config內容的操作步驟
這篇文章主要介紹了C#通過配置文件動態(tài)修改web.config內容的操作步驟,文中通過圖文結合的方式介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-03-03
C#中DataSet,DataTable,DataView的區(qū)別與用法
這篇文章介紹了C#中DataSet,DataTable,DataView的區(qū)別與用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復原窗體的方法
這篇文章主要介紹了C#實現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復原窗體的方法,涉及C#窗體及鼠標事件響應的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08

