生活随笔
收集整理的這篇文章主要介紹了
Visual C#中的(ListBox)数据绑定
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
(2)ListBox組件的數(shù)據(jù)綁定:
ListBox組件的數(shù)據(jù)綁定和ComboBox組件的數(shù)據(jù)綁定的方法大致相同,也是通過設(shè) 定"DisplayMember"、"ValueMember"。其中"DataSource"這三個屬性來完成的。并且這三個屬性在ListBox組件 中代表的意思和ComboBox組件的意思基本一樣。由此可以得到ListBox組件對本地數(shù)據(jù)庫和遠程數(shù)據(jù)庫進行數(shù)據(jù)綁定的源程序。其中 ListBox01.cs是對本地數(shù)據(jù)庫進行數(shù)據(jù)綁定,ListBox02.cs是對遠程數(shù)據(jù)庫進行數(shù)據(jù)綁定,具體如下:
ListBox01.cs源程序代碼節(jié)選:
| public class Form1 : Form { private ListBox ListBox1 ; private Button button1 ; private System.Data.DataSet myDataSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) { file://打開數(shù)據(jù)鏈接,得到數(shù)據(jù)集 GetConnect ( ) ; InitializeComponent ( ) ; } file://清除程序中使用過的資源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void GetConnect ( ) { file://創(chuàng)建一個 OleDbConnection string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = db.mdb" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM person " ; file://創(chuàng)建一個 DataSet myDataSet = new DataSet ( ) ; myConn.Open ( ) ; file://用 OleDbDataAdapter 得到一個數(shù)據(jù)集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://把Dataset綁定person數(shù)據(jù)表 myCommand.Fill ( myDataSet , "person" ) ; file://關(guān)閉此OleDbConnection myConn.Close ( ) ;} private void button1_Click ( object sender , System.EventArgs e ) { ListBox1.DataSource = myDataSet ; ListBox1.DisplayMember = "person.xm" ; ListBox1.ValueMember = "person.xm" ; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } } |
以下代碼是ListBox組件對Sql Server 2000數(shù)據(jù)庫進行數(shù)據(jù)綁定的源程序節(jié)選(ListBox02.cs):
| {private ListBox ListBox1 ; private Button button1 ; private System.Data.DataSet myDataSet ; private System.ComponentModel.Container components = null ; public Form1 ( ) { file://打開數(shù)據(jù)鏈接,得到數(shù)據(jù)集 GetConnect ( ) ; InitializeComponent ( ) ; } file://清除程序中使用過的資源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void GetConnect ( ) { // 設(shè)定數(shù)據(jù)連接字符串,此字符串的意思是打開Sql server數(shù)據(jù)庫, 服務(wù)器名稱為server1,數(shù)據(jù)庫為data1 string strCon = "Provider = SQLOLEDB.1 ; Persist Security Info = False ; User ID = sa ; Initial Catalog = data1 ; Data Source = server1 " ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; myConn.Open ( ) ; string strCom = " SELECT * FROM person " ; file://創(chuàng)建一個 DataSet myDataSet = new DataSet ( ) ; file://用 OleDbDataAdapter 得到一個數(shù)據(jù)集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://把Dataset綁定person數(shù)據(jù)表 myCommand.Fill ( myDataSet , " person " ) ; file://關(guān)閉此OleDbConnection myConn.Close ( ) ; } private void button1_Click ( object sender , System.EventArgs e ) { ListBox1.DataSource = myDataSet ; ListBox1.DisplayMember = "person.xm" ; ListBox1.ValueMember = "person.xm" ; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; }} |
六、總結(jié)
本文介紹的實現(xiàn)數(shù)據(jù)綁定組件的都是在程序設(shè)計中經(jīng)常用到的WinForm組件。當(dāng)然在.Net FrameWork SDK中提供的WinForm組件是很多的,由于本文的限制,不可能一一介紹,一般來說,WinForm組件都可以實現(xiàn)數(shù)據(jù)綁定,雖然在某些具體的方法上 有所差異,但也總是大同小異。在以下的文章中,將以此為基礎(chǔ)探討Visual C#中數(shù)據(jù)庫編程。
轉(zhuǎn)載于:https://www.cnblogs.com/yongbin621/archive/2009/03/27/1422927.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的Visual C#中的(ListBox)数据绑定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。