2013-03-22 52 views
1

我有2種形式,並且在兩種形式的每一種中有DataGridViewchatform和)。如何從不同的窗體中獲取DataGridView.SelectedRows()。Cells()的值?

chatform中,我創建了一個DataGridView,其中每行都生成了Button

每個Button點擊時會加載一個形式,並且當形式我想在始發chatformSelectedRow.CellDataGridView值。

碼(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在不會從數據庫中加載任何數據。

我沒有觀察到任何錯誤,我只是沒有解決方案。

回答

0

第一個問題是您正在調用該類而不是實例。 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查詢字符串值的類。通過這種方式,當您在代碼中創建表單的新實例時,您始終可以傳入搜索字符串,這會導致您要查看的詳細信息。

+0

您好Pow-lan thx是指南,很好,我得到了selectedrow,但prodetail.form中的datagridview1仍然沒有加載任何內容,是我必須調用一些參數到您教我的構造函數〜! – 2013-03-23 00:03:06

+0

hi @ Pow-lan您發佈的解決方案對我來說並不是真正的工作prodetail.form中的datagridview仍然沒有加載任何內容,也沒有觀察到任何錯誤,我真的需要幫助請指導我〜! – 2013-03-23 04:19:34

+0

我會首先放入一些斷點,然後查看第一個查詢是否返回結果,檢查SearchValue是否是您期望的結果。確保正在填充'tbl'變量。等等。首先應該調用構造函數,然後在顯示錶單時,將調用「Load」,除非SQL出現問題,否則應該看到一些東西。 – 2013-03-23 14:51:10

相關問題