Share vb.net datagridview selected row,

anhduy148

New member
#vb.net #Datagridview #SelectedRow #.net #WindowsForms ## Cách lấy hàng đã chọn trong DataGridView trong VB.NET

Trong hướng dẫn này, bạn sẽ tìm hiểu làm thế nào để có được hàng đã chọn trong DataGridView trong VB.NET.Chúng tôi sẽ bao gồm hai phương thức: Sử dụng thuộc tính InSel Index và sử dụng thuộc tính được chọn.

** Sử dụng thuộc tính Indel Index **

Thuộc tính IndelIndex trả về chỉ mục của hàng đã chọn trong DataGridView.Để nhận hàng đã chọn, bạn có thể sử dụng mã sau:

`` `VBNet
Dim đã chọnRowindex as Integer = dgGridView1.SelectedIndex
`` `

Khi bạn có chỉ mục hàng đã chọn, bạn có thể sử dụng nó để truy cập dữ liệu trong hàng.Ví dụ: để lấy văn bản của cột đầu tiên trong hàng đã chọn, bạn có thể sử dụng mã sau:

`` `VBNet
Dim đã chọnRowText dưới dạng chuỗi = dgGridView1.Rows (chọnRowIndex) .Cells (0) .Text
`` `

** Sử dụng thuộc tính được chọn **

Thuộc tính được chọn trả về đối tượng DataGridViewRow cho hàng đã chọn.Để nhận hàng đã chọn, bạn có thể sử dụng mã sau:

`` `VBNet
Dim đã chọnRow dưới dạng datagridviewrow = dggridview1.SelectedItem
`` `

Khi bạn có đối tượng hàng đã chọn, bạn có thể truy cập dữ liệu theo cùng một cách mà bạn sẽ truy cập dữ liệu trong một hàng DataTable.Ví dụ: để lấy văn bản của cột đầu tiên trong hàng đã chọn, bạn có thể sử dụng mã sau:

`` `VBNet
Dim đã chọnRowText dưới dạng chuỗi = chọnRow.cells (0) .Text
`` `

**Ví dụ**

Ví dụ sau đây cho thấy cách lấy hàng đã chọn trong DataGridView trong VB.NET.Ví dụ sử dụng thuộc tính được chọn Index để lấy chỉ mục hàng đã chọn.Mã sau đó sử dụng thuộc tính hàng để lấy đối tượng DataGridViewRow cho hàng đã chọn.Mã sau đó in văn bản của cột đầu tiên trong hàng đã chọn vào bảng điều khiển.

`` `VBNet
Private Sub BtngetSelectedRow_Click (Người gửi dưới dạng Đối tượng, E As EventArgs) xử lý btngetSelectedRow.Click

'Nhận chỉ mục hàng đã chọn.
Dim đã chọnRowindex as Integer = dgGridView1.SelectedIndex

'Nhận đối tượng hàng đã chọn.
Dim đã chọnRow dưới dạng datagridviewrow = dggridview1.Rows (chọnRowIndex)

'In văn bản của cột đầu tiên trong hàng đã chọn vào bảng điều khiển.
Console.WriteLine (chọnRow.Cells (0) .Text)

Kết thúc phụ
`` `

**Phần kết luận**

Trong hướng dẫn này, bạn đã học cách lấy hàng đã chọn trong một datagridview trong vb.net.Chúng tôi đã đề cập đến hai phương thức: sử dụng thuộc tính được chọn và sử dụng thuộc tính được chọn.Bạn có thể sử dụng một trong hai phương thức để có được hàng đã chọn trong DataGridView.
=======================================
#vb.net #Datagridview #SelectedRow #.net #WindowsForms ## How to Get the Selected Row in a DataGridView in VB.NET

In this tutorial, you will learn how to get the selected row in a DataGridView in VB.NET. We will cover two methods: using the SelectedIndex property and using the SelectedItem property.

**Using the SelectedIndex Property**

The SelectedIndex property returns the index of the selected row in a DataGridView. To get the selected row, you can use the following code:

```vbnet
Dim selectedRowIndex As Integer = dgGridView1.SelectedIndex
```

Once you have the selected row index, you can use it to access the data in the row. For example, to get the text of the first column in the selected row, you can use the following code:

```vbnet
Dim selectedRowText As String = dgGridView1.Rows(selectedRowIndex).Cells(0).Text
```

**Using the SelectedItem Property**

The SelectedItem property returns the DataGridViewRow object for the selected row. To get the selected row, you can use the following code:

```vbnet
Dim selectedRow As DataGridViewRow = dgGridView1.SelectedItem
```

Once you have the selected row object, you can access the data in the row in the same way that you would access the data in a DataTable row. For example, to get the text of the first column in the selected row, you can use the following code:

```vbnet
Dim selectedRowText As String = selectedRow.Cells(0).Text
```

**Example**

The following example shows how to get the selected row in a DataGridView in VB.NET. The example uses the SelectedIndex property to get the selected row index. The code then uses the Rows property to get the DataGridViewRow object for the selected row. The code then prints the text of the first column in the selected row to the console.

```vbnet
Private Sub btnGetSelectedRow_Click(sender As Object, e As EventArgs) Handles btnGetSelectedRow.Click

' Get the selected row index.
Dim selectedRowIndex As Integer = dgGridView1.SelectedIndex

' Get the selected row object.
Dim selectedRow As DataGridViewRow = dgGridView1.Rows(selectedRowIndex)

' Print the text of the first column in the selected row to the console.
Console.WriteLine(selectedRow.Cells(0).Text)

End Sub
```

**Conclusion**

In this tutorial, you learned how to get the selected row in a DataGridView in VB.NET. We covered two methods: using the SelectedIndex property and using the SelectedItem property. You can use either method to get the selected row in a DataGridView.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top