最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • Unity 實(shí)現(xiàn)貼花效果的制作教程

    Unity 實(shí)現(xiàn)貼花效果的制作教程

    有些游戲中的戰(zhàn)斗痕跡的效果會通過貼花來實(shí)現(xiàn)的,貼花的方式多種多樣。而在Unity中,有一種給官方文檔提供代碼的解決方案。本文將這些代碼的基礎(chǔ)上做一個繪圖的貼花效果,感興趣的童鞋可以參考一下
    2021-11-11
  • C#中yield return用法分析

    C#中yield return用法分析

    這篇文章主要介紹了C#中yield return用法,對比使用yield return與不使用yield return的流程,更直觀的分析了yield return的用法,需要的朋友可以參考下
    2014-09-09
  • C#資源釋放方法實(shí)例分析

    C#資源釋放方法實(shí)例分析

    這篇文章主要介紹了C#資源釋放方法,結(jié)合實(shí)例詳細(xì)分析了C#資源釋放的具體方法與相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • C# 多線程更新界面的錯誤的解決方法

    C# 多線程更新界面的錯誤的解決方法

    這篇文章主要介紹了C# 多線程更新界面的錯誤方法,由于一個線程的程序,如果調(diào)用一個功能是阻塞的,那么就會影響到界面的更新,導(dǎo)致使用人員操作不便。所以往往會引入雙線程的工作的方式,主線程負(fù)責(zé)更新界面和調(diào)度,而次線程負(fù)責(zé)做一些阻塞的工作,便有了下面春雨里方法
    2021-10-10
  • C#自定義繁體和簡體字庫實(shí)現(xiàn)中文繁體和簡體之間轉(zhuǎ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# 如何用組合替代繼承

    c# 如何用組合替代繼承

    這篇文章主要介紹了c# 如何用組合替代繼承,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-02-02
  • C#替換Word文檔中的書簽內(nèi)容的詳細(xì)步驟

    C#替換Word文檔中的書簽內(nèi)容的詳細(xì)步驟

    Word書簽不但可以幫助讀者快速跳轉(zhuǎn)到想要查看的位置,而且可以用做“占位符”,本文總結(jié)了一個將常見的Word文檔元素替換進(jìn)書簽的C#解決方案,希望對大家有所幫助
    2025-08-08
  • C#數(shù)值轉(zhuǎn)換-隱式數(shù)值轉(zhuǎn)換表參考

    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ù)

    這篇文章介紹了C#使用System.Buffer以字節(jié)數(shù)組Byte[]操作基元類型數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C#中的IEnumerable接口深入研究

    C#中的IEnumerable接口深入研究

    這篇文章主要介紹了.NET中的IEnumerable接口深入研究,分析出了它的實(shí)現(xiàn)原理和實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2014-07-07

最新評論

邵阳市| 馆陶县| 沙河市| 察隅县| 祁连县| 拉萨市| 安新县| 老河口市| 应城市| 博客| 祁门县| 萨嘎县| 藁城市| 长丰县| 河源市| 星座| 黄浦区| 阜平县| 墨玉县| 包头市| 长垣县| 江川县| 洛川县| 沅陵县| 洞口县| 芜湖市| 乐都县| 林州市| 龙海市| 大余县| 天长市| 景洪市| 萨嘎县| 乾安县| 霸州市| 宜章县| 尤溪县| 梅河口市| 凌源市| 新民市| 于田县|