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

Example Analysis of VB.NET processing data Row

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

Share

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

VB.NET processing data row example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

For programmers, it is self-evident that using VB.NET can bring them benefits. So what are its advantages that will attract programmers' attention? Today we can experience the benefits of this language through our understanding of some of the relevant operations of VB.NET in dealing with data rows.

Data-bound list boxes and combo boxes in Windows forms save time. The typical code is as follows (assuming that a SqlDataAdapter or other part has been established to obtain data):

Dim ds As New DataSet ()

SqlDataAdapter1.Fill (ds

"Customers")

ListBox1.DataSource =

Ds.Tables ("Customers")

ListBox1.DisplayMember =

"CompanyName"

ListBox1.ValueMember =

"CustomerID"

In this case, the VB.NET code that processes the rows of data uses the customer records of the Northwind database. The DisplayMember property is set to the record field you want the user to see in the list box, which is the CompanyName of the customers table. Usually the ValueMember property is set to a key field in the data table, which is CustomerID. CustomerID for customer. Once the user has selected a row in the list box, it is easy to get the key field using the SelectedValue property of the list box:

MsgBox (ListBox1.SelectedValue)

However, you may need a reference to the entire data row object associated with the selected item. For example, if the selected row needs to be deleted, the key is not known. You need a reference to the data row to use the Delete method.

A typical Visual Basic developer usually thinks, "I've got the key for this line, and I'm going to write some logic to find the line that uses it." This can be done, but there is a better way to implement it. You can use one line of code to get the rows of data associated with the options in the list box:

Dim dr As DataRow =

CType (ListBox1.SelectedItem

DataRowView). Row

Usually this logic does not appear intuitively, even for experienced developers. To explain how this is done, I split the above line into several lines, and the following VB.NET code handles the data lines in the same way as the above code:

Dim drv As DataRowView

Drv = CType (ListBox1.

SelectedItem, DataRowView)

Dim dr As DataRow

Dr = drv.Row

The DataRowView class is a wrapper for rows of data and is used by multiple Windows forms controls. It makes it easier to display data related to rows of data in the control. When the list box is data bound to the data table (assuming that some rows in the list box are currently selected), the SelectedItem property of the list box holds a DataRowView object.

How to operate the VB.NET stream correctly

Interpretation of two ways of sending E-mail by VB.NET

Discussion on the example of VB.NET calling WinAPI

Introduction to the solution of VB.NET memory occupation

Correct application rules for nesting of VB.NET forms

This means that we can convert the SelectedItem property of the list box to the DataRowView object, which is achieved in the second line of the code above. DataRowView then exposes a Row attribute that points to the wrapped data row. The above code declares a row of data and sets the Row property.

The technique of converting the type of an object to access its interface is not often used in Visual Basic 6.0, but it is common in Visual Basic .NET. With the above example, most experienced developers quickly catch up with this technology.

References to data rows (dr) can be used to maintain rows in any way. It is possible to access any specific field in the data row. The data in a row can be changed to enable the Delete method of the data row to identify the row as deleted, or to delete the row from the rowset of the data table. The following code flag deletes a line:

Dr.Delete ()

Using the primary key (returned by ListBox.SelectedValue) to find the underlying rows of data requires a lot of code, takes a long time, and is slower to execute. It is normal for programmers who are just starting to use Visual Basic .NET to spend hours coding. Understanding the above VB.NET processing techniques for data rows saves a lot of time, making it simpler and easier to maintain the code.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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