Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VB.NET Data Grid

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use VB.NET Data Grid, I hope you will get something after reading this article. Let's discuss it together.

Master / slave Datasheet view is a very common design pattern in database-centric applications. In this mode, the * data (master table) is displayed in a user interface element (such as VB.NET Data Grid), while the related detail data (slave table) is updated accordingly according to the selection of the master table. For example, we can look at the information list of published books by publishers, and when we select a book, the sales of that book are immediately displayed from the table.

There are generally two ways to realize the master / slave data table mode in VB.NET Data Grid, one is to load two related data tables with one VB.NET Data Grid control, and the other is to use two VB.NET Data Grid controls to realize the master / slave mode. For ease of illustration, we use the Titles table and Sales table of the Pubs database that comes with SQL Server 2000 to demonstrate, in which the titles table records the book catalogue of a publishing house, and the Sales table is the sales data of each book, and both tables contain title_id columns.

Load multiple related tables in one VB.NET Data Grid

We all know that the Data Source attribute of VB.NET Data Grid can be bound with Dataset, you can display the data table in VB.NET Data Grid, but many beginners do not know: by adding multiple Data Table objects to Dataset, each Data Table object is loaded into a table, and then set the association, you can display multiple associated tables in VB.NET Data Grid.

Imports System.Data.SqlClient

'reference the SqlClient namespace

Public Class Form1

Const Connection String As String = "integrated security=sspi

Initial catalog=Pubs; data source= (local) "

'define the connection string

Private Sub Button1_Click (By Val sender As Object

By Val e As System.EventArgs) Handles Button1.Click

Dim cn As New SqlConnection (Connection String)

Cn. Open ()

'establish a connection to the database

Dim ds As New Dataset ("Book Sales")

'create a Dataset object

Dim Titles Table As New Data Table ("Titles")

Ds.Tables.Add (Titles Table)

'create a Data Table object (Titles table) and join the Dataset

Dim GetTitlesString As String = "Select * From Titles"

Dim da As New SqlDataAdapter (GetTitlesString, cn)

Da.Fill (Titles Table)

'populate Data Table

Da.Dispose ()

Dim Sales Table As New Data Table ("Sales")

Ds.Tables.Add (Sales Table)

Dim GetSalesString As String = "Select * From Sales"

Da = New SqlDataAdapter (GetSalesString, cn)

Da.Fill (Sales Table)

'create and populate the Sales table

Da.Dispose ()

Cn.Close ()

Dim relation As New Data Relation ("Title Sales", TitlesTable.

Columns ("title_id"), SalesTable.Columns ("title_id"))

Ds.Relations.Add (relation)

Establish the association of two tables based on the title_id column

DataGrid1.DataSource = ds

Bind DataGrid1 to a Dataset object (that is, ds)

End Sub

End Class

It should be noted that in single-table mode, when you populate Dataset with SqlDataAdapter objects, if you find that there is no table in Dataset, you will automatically create a new Data Table and fill it; when using master-slave mode, you should show that each Data Table object is created and populated, and then use the Data Relation object to establish an association between multiple Data Table, and * bind the VB.NET Data Grid to the Dataset.

Run the project and you will see a view with a plus button. Click the plus sign and all loaded table names appear in the form. If you look at the Titles table, you will find that there is a small plus button next to each row. Click the plus sign to see its sales.

On the upper right side of the form, the small left arrow button is used for fallback. Every time we check the sales situation, we have to use the back button to return to the master table. Obviously, this master / slave table mode is not very operable and does not quite conform to our operating habits. Perhaps the latter approach is a better solution.

After reading this article, I believe you have a certain understanding of "how to use VB.NET Data Grid". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report