Share vb.net search datagridview using textbox,

ngochuecherries

New member
#vb.net #Datagridview #search #TextBox #tutorial

## Cách tìm kiếm DataGridView trong VB.NET bằng cách sử dụng hộp văn bản

Trong hướng dẫn này, bạn sẽ tìm hiểu cách tìm kiếm DataGridView trong VB.NET bằng hộp văn bản.Bạn sẽ học cách:

* Liên kết DataGridView với nguồn dữ liệu
* Thêm một hộp văn bản vào biểu mẫu của bạn
* Liên kết hộp văn bản với bộ sưu tập cột của DataGridView
* Thêm nút vào biểu mẫu của bạn để kích hoạt tìm kiếm
* Viết mã để tìm kiếm DataGridView

### 1. Liên kết DataGridView với nguồn dữ liệu

Bước đầu tiên là liên kết DataGridView với nguồn dữ liệu.Trong ví dụ này, chúng tôi sẽ sử dụng một dữ liệu làm nguồn dữ liệu của chúng tôi.

Để liên kết DataGridView với nguồn dữ liệu, bạn có thể sử dụng thuộc tính `DataSource`.Thuộc tính `DataSource` lấy một` System.Data.DataSet`, `System.Data.DataTable`,` System.Data.DataView` hoặc `System.Data.ibindingList` đối tượng là giá trị của nó.

Trong ví dụ này, chúng tôi sẽ tạo một dữ liệu mới và thêm một số dữ liệu vào nó.Sau đó, chúng tôi sẽ liên kết DataGridView với DataTable.

`` `VBNet
Dim DT như DataTable mới
dt.columns.add ("tên", gettype (chuỗi))
dt.columns.add ("tuổi", gettype (số nguyên))
dt.rows.add ("John", 20)
dt.Rows.Add ("Jane", 21)
dt.rows.add ("Mary", 22)

Me.datagridview1.datasource = dt
`` `

### 2. Thêm hộp văn bản vào biểu mẫu của bạn

Bước tiếp theo là thêm một hộp văn bản vào biểu mẫu của bạn.Bạn có thể làm điều này bằng cách sử dụng phương thức `add` của bộ sưu tập` controls`.

`` `VBNet
Me.controls.add (Textbox mới ())
`` `

### 3. Liên kết hộp văn bản với bộ sưu tập cột của DataGridView

Bước tiếp theo là liên kết hộp văn bản với bộ sưu tập cột của DataGridView.Điều này sẽ cho phép chúng tôi sử dụng hộp văn bản để lọc dữ liệu trong datagridview.

Để liên kết hộp văn bản với bộ sưu tập cột của DatagridView, bạn có thể sử dụng thuộc tính `DataBindings`.Thuộc tính `Databindings` lấy một đối tượng` system.windows.forms.binding` làm giá trị của nó.

Trong ví dụ này, chúng tôi sẽ liên kết hộp văn bản với cột `name` của datagridview.

`` `VBNet
Me.textbox1.databindings.add (BINDING MỚI ("Text", me.datagridview1.columns ("name"), dataSourCemode.twoway))
`` `

### 4. Thêm một nút vào biểu mẫu của bạn

Bước tiếp theo là thêm một nút vào biểu mẫu của bạn.Nút này sẽ kích hoạt tìm kiếm.

Để thêm một nút vào biểu mẫu của bạn, bạn có thể sử dụng phương thức `Thêm` của bộ sưu tập` controls`.

`` `VBNet
Me.controls.add (nút mới ())
`` `

### 5. Viết mã để tìm kiếm DataGridView

Bước cuối cùng là viết mã để tìm kiếm DataGridView.Mã để tìm kiếm DataGridView rất đơn giản.

`` `VBNet
Nút phụ riêng1_click (người gửi dưới dạng đối tượng, e là EventArgs) Nút tay cầm1.Click
'Nhận văn bản từ hộp văn bản.
Dim văn bản dưới dạng chuỗi = me.textbox1.text

'Lọc dữ liệu trong datagridview.
Me.datagridview1.filter = text
Kết thúc phụ
`` `

### Bản tóm tắt

Trong hướng dẫn này, bạn đã học cách tìm kiếm DataGridView trong VB.NET bằng hộp văn bản.Bạn đã học được cách:

* Liên kết DataGridView với nguồn dữ liệu
* Thêm một hộp văn bản vào biểu mẫu của bạn
* Liên kết hộp văn bản với bộ sưu tập cột của DataGridView
* Thêm nút vào biểu mẫu của bạn
* Viết mã để tìm kiếm DataGridView

Tôi hy vọng hướng dẫn này là hữu ích.Nếu bạn có câu hỏi nào,
=======================================
#vb.net #Datagridview #search #TextBox #tutorial

## How to Search a DataGridView in VB.NET Using a Textbox

In this tutorial, you will learn how to search a DataGridView in VB.NET using a textbox. You will learn how to:

* Bind a DataGridView to a data source
* Add a textbox to your form
* Bind the textbox to the DataGridView's Columns collection
* Add a button to your form to trigger the search
* Write the code to search the DataGridView

### 1. Bind a DataGridView to a data source

The first step is to bind a DataGridView to a data source. In this example, we will use a DataTable as our data source.

To bind a DataGridView to a data source, you can use the `DataSource` property. The `DataSource` property takes a `System.Data.DataSet`, `System.Data.DataTable`, `System.Data.DataView`, or `System.Data.IBindingList` object as its value.

In this example, we will create a new DataTable and add some data to it. We will then bind the DataGridView to the DataTable.

```vbnet
Dim dt As New DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Age", GetType(Integer))
dt.Rows.Add("John", 20)
dt.Rows.Add("Jane", 21)
dt.Rows.Add("Mary", 22)

Me.dataGridView1.DataSource = dt
```

### 2. Add a textbox to your form

The next step is to add a textbox to your form. You can do this by using the `Add` method of the `Controls` collection.

```vbnet
Me.Controls.Add(New TextBox())
```

### 3. Bind the textbox to the DataGridView's Columns collection

The next step is to bind the textbox to the DataGridView's Columns collection. This will allow us to use the textbox to filter the data in the DataGridView.

To bind the textbox to the DataGridView's Columns collection, you can use the `DataBindings` property. The `DataBindings` property takes a `System.Windows.Forms.Binding` object as its value.

In this example, we will bind the textbox to the `Name` column of the DataGridView.

```vbnet
Me.textBox1.DataBindings.Add(New Binding("Text", Me.dataGridView1.Columns("Name"), DataSourceMode.TwoWay))
```

### 4. Add a button to your form

The next step is to add a button to your form. This button will trigger the search.

To add a button to your form, you can use the `Add` method of the `Controls` collection.

```vbnet
Me.Controls.Add(New Button())
```

### 5. Write the code to search the DataGridView

The final step is to write the code to search the DataGridView. The code to search the DataGridView is very simple.

```vbnet
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
' Get the text from the textbox.
Dim text As String = Me.textBox1.Text

' Filter the data in the DataGridView.
Me.dataGridView1.Filter = text
End Sub
```

### Summary

In this tutorial, you learned how to search a DataGridView in VB.NET using a textbox. You learned how to:

* Bind a DataGridView to a data source
* Add a textbox to your form
* Bind the textbox to the DataGridView's Columns collection
* Add a button to your form
* Write the code to search the DataGridView

I hope this tutorial was helpful. If you have any questions,
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top