0

好的,这似乎应该很容易做到,但是我似乎无法在不出错的情况下工作。我有一个 IfThen 语句,它说明是否选择了一行,然后执行,否则显示消息。如果我选择一行,代码运行良好,但是当我尝试在不选择行的情况下继续时,我得到一个错误而不是我想要显示的消息。这是我正在尝试做的一个例子:

Protected Sub btnReplace(ByVal sender As Object, ByVal e As EventArgs)

    Dim row As GridViewRow = device_list.SelectedRow

    If (row.RowState Or DataControlRowState.Selected) > 0 Then
        Message.Text = "You selected " & row.Cells(1).Text & "."
    Else
        Message.Text = "Please select a device."
    End If

End Sub

谁能帮我解决这个问题?

4

2 回答 2

1

正如 Miguel 已经提到的,当没有选择任何行时,SelectedRow 属性不返回任何内容。所以你必须检查这个:

Dim row As GridViewRow = device_list.SelectedRow
If Not row Is Nothing Then
   Message.Text = "You selected " & row.Cells(1).Text & "."
Else
    Message.Text = "Please select a device."
End If

您还可以检查 SelectedIndex 是否为 <> -1

 If device_list.SelectedIndex <> -1 Then
    Message.Text = "You selected " & device_list.SelectedRow.Cells(1).Text & "."
 Else
     Message.Text = "Please select a device."
 End If
于 2010-12-23T00:44:53.120 回答
0

如果没有选择行,这是否意味着“行”将评估为 Nothing,因此当您尝试获取它的 RowState 时会出现错误,因为 Nothing 不能有 RowStates?

我会检查它是否是这样的:

Protected Sub btnReplace(ByVal sender As Object, ByVal e As EventArgs)

    Dim row As GridViewRow = device_list.SelectedRow

    If (row <> Nothing Or DataControlRowState.Selected) > 0 Then
        Message.Text = "You selected " & row.Cells(1).Text & "."
    Else
        Message.Text = "Please select a device."
    End If

End Sub
于 2010-12-23T00:25:39.807 回答