自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

講述VB.NET Data Grid實(shí)現(xiàn)主/從數(shù)據(jù)表

開(kāi)發(fā) 后端
這里介紹VB.NET Data Grid實(shí)現(xiàn)主/從數(shù)據(jù)表,包括介紹利用主/從數(shù)據(jù)表模式,能在較小的編程工作量下,實(shí)現(xiàn)很好的顯示及操作效果。

在向大家詳細(xì)介紹VB.NET Data Grid之前,首先讓大家了解下實(shí)現(xiàn)DataGrid2的內(nèi)容動(dòng)態(tài)更新,然后全面介紹VB.NET Data Grid。

VB.NET Data Grid實(shí)現(xiàn)主/從數(shù)據(jù)表

更有效的解決方案是使用兩個(gè)VB.NET Data Grid控件,主、從表均可見(jiàn)。對(duì)主表上某行進(jìn)行選擇,會(huì)立即引發(fā)從表內(nèi)容的改變。

建立工程,添加一個(gè)Panel控件,將其Dock屬性設(shè)置為Top;添加一個(gè)Splitter控件,Dock屬性同樣設(shè)置為Top;在窗體下部再添加一個(gè) Panel,Dock屬性為top.然后,在兩面板中各添加一個(gè)VB.NET Data Grid,其Dock屬性為Fill.

要實(shí)現(xiàn)DataGrid2的內(nèi)容動(dòng)態(tài)更新,需要對(duì)DataGrid1的CurrentCellChanged事件進(jìn)行監(jiān)聽(tīng),在接受到DataGrid1的變化消息后,加載相應(yīng)的數(shù)據(jù)。

  1. Imports System.Data.SqlClient  
  2. Public Class Form1  
  3. Const Connection String As String = "integrated security=sspi;initial catalog=pubs; 
  4. data source=(local)" 
  5. Private Sub Button1_Click(By Val sender As Object, 
    By Val e As System.EventArgs) Handles Button1.Click  
  6. Dim cn As New SqlConnection(Connection String)  
  7. cn.Open()  
  8. Dim ds As New Dataset  
  9. Dim GetTitlesString As String = "Select * From Titles" 
  10. Dim Titles Table As New Data Table("Titles")  
  11. ds.Tables.Add(Titles Table)  
  12. Dim da As New SqlDataAdapter(GetTitlesString, cn)  
  13. da.Fill(Titles Table)  
  14. da.Dispose()  
  15. cn.Close()  
  16. DataGrid1.DataSource = Titles Table  
  17. ’主表顯示在DataGrid1中  
  18. End Sub  
  19. Private Sub DataGrid1_CurrentCellChanged(By Val sender As Object, 
    By Val e As System.EventArgs) Handles DataGrid1.CurrentCellChanged  
  20. Dim titled As String = DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, 0).To String  
  21. ’判斷用戶在主表中選擇了哪一行,取出它的第0列(在本例中即為title_id列)  
  22. Dim sql As String = "select * from sales where title_id=’" & titled & "’"  
  23. ’SQL命令字符串,選擇與主表中相同title_id值的從表數(shù)據(jù)  
  24. Dim cn As New SqlConnection(Connection String)  
  25. cn.Open()  
  26. Dim ds As New Dataset  
  27. Dim da As New SqlDataAdapter(sql, cn)  
  28. Dim Sales Table As New Data Table("Sales")  
  29. ds.Tables.Add(Sales Table)  
  30. da.Fill(Sales Table)  
  31. ’用選擇的從表數(shù)據(jù)填充,更新  
  32. da.Dispose()  
  33. cn.Close()  
  34. DataGrid2.DataSource = Sales Table  
  35. End Sub  
  36. End Class 

運(yùn)行程序,在主表中選擇某行,從表就會(huì)顯示出匹配的銷售信息。

利用主/從數(shù)據(jù)表模式,能在較小的編程工作量下,實(shí)現(xiàn)很好的顯示及操作效果。若要建立多表關(guān)聯(lián)的主/從視圖,或是進(jìn)行增、刪、改等操作,在此方法上進(jìn)行改進(jìn)即可。希望本文能給讀者在數(shù)據(jù)庫(kù)編程時(shí)帶來(lái)一定的啟示和幫助。

【編輯推薦】

  1. 淺談VB.NET線程構(gòu)造器
  2. 簡(jiǎn)單分析VB.NET使用線程
  3. VB.NET List(T)編寫框架方法
  4. 簡(jiǎn)單介紹VB.NET線程同步
  5. VB.NET聲明API詳細(xì)描述
責(zé)任編輯:佚名 來(lái)源: ITPUB
相關(guān)推薦

2009-10-12 13:54:22

VB.NET Data

2009-10-29 09:57:16

VB.NET實(shí)現(xiàn)數(shù)據(jù)綁

2009-10-23 13:22:25

VB.NET實(shí)現(xiàn)拖動(dòng)圖

2009-10-21 18:28:48

VB.NET表間拖放

2009-10-14 17:08:44

VB.NET使用Fil

2009-10-16 13:26:53

VB.NET Exce

2009-10-21 10:45:50

VB.NET Quic

2009-10-12 16:39:59

OracleTransVB.NET使用

2009-10-19 08:55:22

VB.NET多重繼承

2009-10-13 17:03:55

VB.NET面向?qū)ο?/a>

2009-11-02 15:45:03

VB.NET IEnu

2009-10-16 09:35:24

VB.NET制作透明窗

2009-10-15 16:39:00

VB.NET讀取INI

2009-10-15 11:11:08

VB.NET Text

2009-10-10 16:44:52

VB.NET開(kāi)發(fā)控件

2009-10-14 11:15:06

VB.NET Grou

2009-10-27 11:39:03

VB.NET事件處理程

2009-10-22 09:20:46

VB.NET Proc

2009-10-26 18:11:47

VB.NET調(diào)用Exc

2009-11-03 17:31:01

VB.NET窗體
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)