如何从不同的形式获取DataGridView.SelectedRows()。Cells()的值?

时间:2013-03-22 13:39:13

标签: vb.net winforms datagridview

我有2个表单,在2个表单的每个表单中都有DataGridViewchatformprodetail)。

chatform我创建了一个DataGridView,每行都生成了Button

点击后每个Button都会加载一个prodetail表单,当我在prodetail表单中时,我想从SelectedRow.Cell获取DataGridView的值在原始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),假设它会显示第一行的数据,但DataGridView1中的prodetail不会从数据库加载任何数据。

我没有发现任何错误,我只是没有解决方案。

1 个答案:

答案 0 :(得分: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类的新实例中显示所单击行的详细信息。

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