简体   繁体   English

在原始表单(vb.net 2013)上的Datagridview中,在对话框表单上自动填充文本框

[英]Auto-fill textbox on a dialog form, from a Datagridview on the original form, vb.net 2013

I am currently working in windows form applications using vb.net 2013. I have two forms, we can call them form1 and form 2 for now. 我目前正在使用vb.net 2013在Windows窗体应用程序中工作。我有两种窗体,我们现在可以将它们分别称为form1和form 2。 Form1 has a datagridview with a checkbox column that the end user will click to open form2 as a dialog form. Form1的datagridview带有一个复选框列,最终用户将单击该复选框以打开form2作为对话框表单。 Once form2 opens I want it to automatically load and fill two text boxes that have information from corresponding columns from the original DGV. 一旦form2打开,我希望它自动加载并填充两个文本框,这些文本框包含来自原始DGV相应列的信息。 In short, the DGV on form1 has 3 columns; 简而言之,form1上的DGV有3列; JobNumber, LineNumber, and the checkbox. JobNumber,LineNumber和复选框。 The end user will click the checkbox to bring up form2 and I want the Jobnumber and Linenumber to automaticaly fill into two textboxes on form2. 最终用户将单击复选框以显示form2,并且我希望Jobnumber和Linenumber自动填充到form2上的两个文本框中。 Here is my code from form1. 这是我来自form1的代码。

        'assembly dialog result form
        dr = f.ShowDialog
        If dr = Windows.Forms.DialogResult.OK Then
            'dim y as job string
            Dim Y As String = DataGridOrdered.Rows(e.RowIndex).Cells(3).Value
            'open connection
            Using conn1 As New SqlConnection(connstring)
                conn1.Open()
                Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET Complete = 1, RackIn = 1 WHERE JobNumber = '" & Y & "'", conn1)
                    comm1.ExecuteNonQuery()
                    conn1.Close()
                End Using
            End Using

            Call DGVOrderedRefresh()

        ElseIf dr = Windows.Forms.DialogResult.Yes Then

            'dim M as job string
            Dim M As String = DataGridOrdered.Rows(e.RowIndex).Cells(3).Value
            'open connection
            Using conn1 As New SqlConnection(connstring)
                conn1.Open()
                Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET Complete = 1, RackIn = 1 WHERE JobNumber = '" & M & "'", conn1)
                    comm1.ExecuteNonQuery()
                    conn1.Close()
                End Using
            End Using

            Call DGVOrderedRefresh()

        ElseIf dr = Windows.Forms.DialogResult.Cancel Then
            'refresh datagridview ordered
            Call DGVOrderedRefresh()

        End If

    ElseIf e.ColumnIndex = 0 Then
        'fail safe to make sure the header is not clicked
        If e.RowIndex = -1 Then
            Exit Sub
        End If

The code for my dialog is as follows 我的对话框的代码如下

Imports System.Data

Imports System.Data.SqlClient 导入System.Data.SqlClient

Public Class BuildName 公共类BuildName

' Dim connstring As String = "DATA SOURCE = BNSigma\CORE; integrated security = true"
Dim connstring As String = "DATA SOURCE = BNSigma\TEST; integrated security = true"

Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click




End Sub

Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim ds As New DataTable
    Try

        'load name combo box
        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As SqlCommand = New SqlCommand("SELECT Name FROM Production.dbo.FCNames", conn1)
                Dim adapater As New SqlDataAdapter
                adapater.SelectCommand = comm1
                adapater.Fill(ds)
                adapater.Dispose()
                conn1.Close()
                CBName.DataSource = ds
                CBName.DisplayMember = "Name"
                CBName.ValueMember = "Name"
            End Using
        End Using


    Catch ex As Exception
        MsgBox("Error loading name List, please contact a mfg. Engr.!")
        MsgBox(ex.ToString)
    End Try


End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'Update built by name
    Dim v As Object = TBFloor.Text
    Dim G As Object = TBLine.Text
    Dim O As Object = TBJobNumber.Text
    Try

    Using conn1 As New SqlConnection(connstring)
        conn1.Open()
            Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET BuiltBy = @Name Where JobNumber = '" & O & "'", conn1)
                comm1.Parameters.AddWithValue("@Name", CBName.Text)
                comm1.ExecuteNonQuery()
                conn1.Close()

            End Using
    End Using
    Catch ex As Exception
        MsgBox("Error updating Ordered table, please contact a MFG. ENGR.!")
        MsgBox(ex.ToString)
    End Try

End Sub

End Class 最终班

UPDATED CODE FOR VALTER Form1 code VALTER Form1代码的更新代码

 Public row_Index As Integer = 0

Private Sub DataGridOrdered_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridOrdered.CurrentCellDirtyStateChanged

    If DataGridOrdered.IsCurrentCellDirty Then
        DataGridOrdered.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

Private Sub DataGridOrdered_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridOrdered.CellValueChanged
    If DataGridOrdered.Columns(e.ColumnIndex).Name = 9 Then

        Dim checkCell As DataGridViewCheckBoxCell = CType(DataGridOrdered.Rows(e.RowIndex).Cells(9), DataGridViewCheckBoxCell)

        If CType(checkCell.Value, [Boolean]) = True Then
            row_Index = e.RowIndex

            BuildName.ShowDialog(Me)
        End If
    End If
End Sub

Form 2 code Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load 表格2的代码Private Sub BuildName_Load(作为对象发送,作为EventArgs发送)处理MyBase.Load

    Dim value1 As Object = FormOrdered.DataGridOrdered.Rows(FormOrdered.row_Index).Cells(3).Value
    Dim value2 As Object = FormOrdered.DataGridOrdered.Rows(FormOrdered.row_Index).Cells(4).Value

    TBJobNumber.Text = CType(value1, String)
    TBFloor.Text = CType(value2, String)

In your form1 add: 在您的form1中添加:

Public Row_Index As Integer = 0 //use a simple variable  

Sub datagridOrdered_CurrentCellDirtyStateChanged( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles datagridOrdered.CurrentCellDirtyStateChanged

    If datagridOrdered.IsCurrentCellDirty Then
        datagridOrdered.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

Private Sub datagridOrdered_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datagridOrdered.CellValueChanged
    If datagridOrdered.Columns(e.ColumnIndex).Name = "Assemble" Then //<- here

        Dim checkCell As DataGridViewCheckBoxCell = _
                  CType(datagridOrdered.Rows(e.RowIndex).Cells(2),  _  //<- here
                  DataGridViewCheckBoxCell)

        If CType(checkCell.Value, [Boolean]) = True Then
            //RowIndex = e.RowIndex you dont need this
            Row_Index = e.RowIndex

            BuildName.ShowDialog(Me)
        End If
    End If
End Sub

//Nor this

//Private _count As Integer

//Public Property RowIndex() As Integer
    //Get
        //Return _count
    //End Get
    //Set(value As Integer)
        //_count = value
    //End Set
//End Property

And in dialog load event use Form1.Row_Index instead: 在对话框加载事件中,请使用Form1.Row_Index代替:

Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim value1 As Object = FormOrdered.datagridOrdered.Rows(Form1.Row_Index).Cells(0).Value //<- here
    Dim value2 As Object = FormOrdered.datagridOrdered.Rows(Form1.Row_Index).Cells(1).Value //<- here

    TBJobNumber.Text = CType(value1, String)
    TBFloor.Text = CType(value2, String)
    ...
    ...
End Sub

or add a module and add the Row_Index there. 或添加一个模块,然后在其中添加Row_Index You can use it then as is 您可以按原样使用它

Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim value1 As Object = FormOrdered.DataGridView1.Rows(Row_Index).Cells(0).Value
    Dim value2 As Object = FormOrdered.DataGridView1.Rows(Row_Index).Cells(1).Value

    TBJobNumber.Text = CType(value1, String)
    TBFloor.Text = CType(value2, String)
    ...
    ...
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM