日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

Visual C#中的(ListBox)数据绑定

發(fā)布時(shí)間:2025/7/14 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Visual C#中的(ListBox)数据绑定 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

(2)ListBox組件的數(shù)據(jù)綁定:

ListBox組件的數(shù)據(jù)綁定和ComboBox組件的數(shù)據(jù)綁定的方法大致相同,也是通過(guò)設(shè) 定"DisplayMember"、"ValueMember"。其中"DataSource"這三個(gè)屬性來(lái)完成的。并且這三個(gè)屬性在ListBox組件 中代表的意思和ComboBox組件的意思基本一樣。由此可以得到ListBox組件對(duì)本地?cái)?shù)據(jù)庫(kù)和遠(yuǎn)程數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)綁定的源程序。其中 ListBox01.cs是對(duì)本地?cái)?shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)綁定,ListBox02.cs是對(duì)遠(yuǎn)程數(shù)據(jù)庫(kù)進(jìn)行數(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://打開(kāi)數(shù)據(jù)鏈接,得到數(shù)據(jù)集
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過(guò)的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}

private void GetConnect ( )
{
file://創(chuàng)建一個(gè) 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)建一個(gè) DataSet
myDataSet = new DataSet ( ) ;

myConn.Open ( ) ;
file://用 OleDbDataAdapter 得到一個(gè)數(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組件對(duì)Sql Server 2000數(shù)據(jù)庫(kù)進(jìn)行數(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://打開(kāi)數(shù)據(jù)鏈接,得到數(shù)據(jù)集
GetConnect ( ) ;
InitializeComponent ( ) ;
}
file://清除程序中使用過(guò)的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}

private void GetConnect ( )
{
// 設(shè)定數(shù)據(jù)連接字符串,此字符串的意思是打開(kāi)Sql server數(shù)據(jù)庫(kù),

服務(wù)器名稱為server1,數(shù)據(jù)庫(kù)為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)建一個(gè) DataSet
myDataSet = new DataSet ( ) ;
file://用 OleDbDataAdapter 得到一個(gè)數(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é)

本文介紹的實(shí)現(xiàn)數(shù)據(jù)綁定組件的都是在程序設(shè)計(jì)中經(jīng)常用到的WinForm組件。當(dāng)然在.Net FrameWork SDK中提供的WinForm組件是很多的,由于本文的限制,不可能一一介紹,一般來(lái)說(shuō),WinForm組件都可以實(shí)現(xiàn)數(shù)據(jù)綁定,雖然在某些具體的方法上 有所差異,但也總是大同小異。在以下的文章中,將以此為基礎(chǔ)探討Visual C#中數(shù)據(jù)庫(kù)編程。

轉(zhuǎn)載于:https://www.cnblogs.com/yongbin621/archive/2009/03/27/1422927.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Visual C#中的(ListBox)数据绑定的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。