我有2種形式,並且在兩種形式的每一種中有DataGridView
(chatform
和)。如何從不同的窗體中獲取DataGridView.SelectedRows()。Cells()的值?
在chatform
中,我創建了一個DataGridView
,其中每行都生成了Button
。
每個Button
點擊時會加載一個形式,並且當形式我想在始發chatform
的SelectedRow.Cell
從DataGridView
值。
碼(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
碼():
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
在不會從數據庫中加載任何數據。
我沒有觀察到任何錯誤,我只是沒有解決方案。
您好Pow-lan thx是指南,很好,我得到了selectedrow,但prodetail.form中的datagridview1仍然沒有加載任何內容,是我必須調用一些參數到您教我的構造函數〜! – 2013-03-23 00:03:06
hi @ Pow-lan您發佈的解決方案對我來說並不是真正的工作prodetail.form中的datagridview仍然沒有加載任何內容,也沒有觀察到任何錯誤,我真的需要幫助請指導我〜! – 2013-03-23 04:19:34
我會首先放入一些斷點,然後查看第一個查詢是否返回結果,檢查SearchValue是否是您期望的結果。確保正在填充'tbl'變量。等等。首先應該調用構造函數,然後在顯示錶單時,將調用「Load」,除非SQL出現問題,否則應該看到一些東西。 – 2013-03-23 14:51:10