从原始表单上的Datagridview vb.net 2013自动填充对话框窗体上的文本框

时间:2014-09-15 17:40:30

标签: sql vb.net datagridview dialog

我目前正在使用vb.net 2013在Windows窗体应用程序中工作。我有两种形式,我们现在可以将它们称为form1和form 2。 Form1有一个datagridview,其中包含一个复选框列,最终用户将单击该列以打开form2作为对话框表单。一旦form2打开,我希望它自动加载并填充两个文本框,这些文本框包含来自原始DGV的相应列的信息。简而言之,form1上的DGV有3列; JobNumber,LineNumber和复选框。最终用户将单击该复选框以显示form2,我希望Jobnumber和Linenumber自动填充到form2上的两个文本框中。这是我在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

我的对话框的代码如下

Imports System.Data

导入System.Data.SqlClient

Public Class 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

结束班

更新代码 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

表格2代码   Private Sub BuildName_Load(sender as Object,e As 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)

1 个答案:

答案 0 :(得分:1)

在你的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

在对话框加载事件中,使用 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

或添加模块并在那里添加 Row_Index 。您可以按原样使用它

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