1

我有 2 种形式,在 2 种形式中的每一种中都有一个DataGridView(chatformprodetail)。

chatform我创建了一个在每一行DataGridView中都有一个生成的。Button

每次Button单击时都会加载一个prodetail表单,并且在prodetail表单中我想SelectedRow.CellDataGridView原始chatform.

代码 ( chatform):

Public Sub loadtoDGV()
    Dim sqlq As String = "SELECT * FROM chattbl"
    Dim sqlcmd As New SqlCommand
    Dim sqladpt As New SqlDataAdapter
    Dim tbl As New DataTable

    With sqlcmd
        .CommandText = sqlq
        .Connection = conn
    End With

    With sqladpt
        .SelectCommand = sqlcmd
        .Fill(tbl)
    End With

    DataGridView1.Rows.Clear()
    For i = 0 To tbl.Rows.Count - 1
        With DataGridView1
            .Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"))
        End With
    Next
    conn.Close()
End Sub

Private Sub ChatForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    loadtoDGV()
End Sub

代码 ( DataGridView1.CellContentClick):

Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
    If colName = "Detail" Then
        Prodetail.Show()
        MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
    End If
End Sub

代码 ( prodetail):

Public Sub loadtoDGV2()

    Dim i As Integer
    i = ChatForm.DataGridView1.SelectedRows.Count
    MsgBox(i)

    Dim compareai As String = ChatForm.DataGridView1.SelectedRows(i).Cells(1).Value
    Dim sqlq As String = "SELECT * FROM Chattbl WHERE Title =" & compareai & ""
    Dim sqlcmd As New SqlCommand
    Dim sqladpt As New SqlDataAdapter
    Dim tbl As New DataTable

    With sqlcmd
        .CommandText = sqlq
        .Connection = conn
    End With

    With sqladpt
        .SelectCommand = sqlcmd
        .Fill(tbl)
    End With

    DataGridView1.Rows.Clear()
    For i = 0 To tbl.Rows.Count - 1
        With DataGridView1
            .Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"), tbl.Rows(i)("ChatContent"))
        End With
    Next
    conn.Close()
End Sub

Private Sub Prodetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    loadtoDGV2()
End Sub

我做错了什么?

我尝试使用MsgBox(i) i = SelectedRow(0)假设它会显示第一行的数据,但不会从数据库中加载任何数据DataGridView1prodetail

我没有观察到任何错误,我只是没有解决方案。

4

1 回答 1

1

第一个问题是您调用的是类而不是实例。VB.NET 将允许您调用表单的一个实例作为其名称,但每次使用时它都是同一个实例。我不建议这样做。

首先我会改变这个:

Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
    If colName = "Detail" Then
        Prodetail.Show()
        MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
    End If
End Sub

对此:

Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
    If colName = "Detail" Then
        Dim newDetailForm as new Proddetail(dataGridView1.Rows(e.RowIndex).Cells(1).Value)
        newDetailForm.show()
        MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
    End If
End Sub

然后在Proddetail类中,您需要添加一个构造函数和一个成员,如下所示:

Private SearchValue as String 

Public Sub New(byval theSearchValue as string)
    InitalizeComponent()

    SearchValue = theSearchValue
End Sub

然后在您的加载例程中:

Public Sub loadtoDGV2()      
    Dim sqlq As String = "SELECT * FROM Chattbl WHERE Title =" & SearchValue & ""
    Dim sqlcmd As New SqlCommand
    Dim sqladpt As New SqlDataAdapter
    Dim tbl As New DataTable

    With sqlcmd
        .CommandText = sqlq
        .Connection = conn
    End With

    With sqladpt
        .SelectCommand = sqlcmd
        .Fill(tbl)
    End With

    DataGridView1.Rows.Clear()
    For i = 0 To tbl.Rows.Count - 1
        With DataGridView1
            .Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"), tbl.Rows(i)("ChatContent"))
        End With
    Next
    conn.Close()
End Sub

这应该会在类的新实例中显示单击行的详细信息Proddetail

我向获取 SQL 查询字符串值的类添加了一个自定义参数化构造函数。这样,当您在代码中创建表单的新实例时,您始终可以传入搜索字符串,该字符串将导致您想要查看的详细信息。

于 2013-03-22T15:52:17.713 回答