vb.net 如何从不同的表单中获取 DataGridView.SelectedRows().Cells() 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15571791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the value of DataGridView.SelectedRows().Cells() from different forms?
提问by u2193314
I have 2 forms and within each of the 2 forms there is a DataGridView(chatformand prodetail).
我有 2 种形式,在 2 种形式中的每一种中都有一个DataGridView(chatform和prodetail)。
In the chatformI created a DataGridViewwhich has a generated Buttonin each row.
在chatform我创建了一个DataGridView,其中Button每行都生成了一个。
Each Buttonwhen clicked will load a prodetailform and when in the prodetailform I want to get the value of the SelectedRow.Cellfrom the DataGridViewin the originating chatform.
每次Button点击时都会加载一个prodetail表单,当在表单中时,prodetail我想SelectedRow.Cell从DataGridView原始chatform.
Code (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
Code (DataGridView1.CellContentClick):
代码 ( 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
Code (prodetail):
代码 ( 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
What have I done wrong?
我做错了什么?
I tried to use MsgBox(i) i = SelectedRow(0)assuming it would show the data for first row, but DataGridView1in prodetaildoes not load any data from the database.
我试图用MsgBox(i) i = SelectedRow(0)假设它会显示第一行的数据,但DataGridView1在prodetail不从数据库中加载任何数据。
I did not observe any errors, I just don't have a solution.
我没有观察到任何错误,我只是没有解决方案。
回答by Pow-Ian
The first problem is you are calling the class instead of an instance. VB.NET will allow you to call an instance of the form as its name, but it will be the same instance across every use. I would not suggest doing this.
第一个问题是您正在调用类而不是实例。VB.NET 将允许您调用表单的一个实例作为其名称,但每次使用时它都是相同的实例。我不建议这样做。
To start I would change this:
首先,我会改变这个:
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
To this:
对此:
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
Then in the Proddetailclass you need to add a constructor and a member like this:
然后在Proddetail类中,您需要添加一个构造函数和一个成员,如下所示:
Private SearchValue as String
Public Sub New(byval theSearchValue as string)
InitalizeComponent()
SearchValue = theSearchValue
End Sub
Then in your load routine:
然后在你的加载例程中:
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
This should then display the details of the clicked row in a new instance of the Proddetailclass.
然后应该在Proddetail类的新实例中显示单击行的详细信息。
I added a custom paramaterizedconstructor to the class that takes the value for the SQL query string. This way when you create a new instance of the form in your code, you can always pass in the search string that would lead to the details you want to view.
我向类添加了一个自定义参数化构造函数,该类采用 SQL 查询字符串的值。这样,当您在代码中创建表单的新实例时,您始终可以传入搜索字符串,该字符串将指向您要查看的详细信息。

