winforms 向SQL填充的datagrid列添加组合框值

cunj1qz1  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(214)

我很难找到这个问题的答案。我有一个Windows应用程序,操作员在上面扫描条形码,应用程序根据条形码从SQL数据库返回一些信息。然后向DataGridView控件添加一行。
我被要求做一个改变-在同一个表单上有一个组合框控件,有4个静态配置的值。我需要发生的是,当操作员扫描条形码和新的一行被添加到DGV控件,在DGV的最后一列需要填充选择的CB控件的项目。
截图A:DGV在扫描条形码后的外观:

截图B:我想要达到的目标;注意TestPanel列文本与Panel组合框内容相匹配:

我在搜索时找到的大多数答案都建议在DGV被填充之前将数据添加到数据表中--我在网上尝试了一些建议,但没有什么乐趣。不确定这是错误的解决方案还是我做错了什么。
我也给了下面的一个尝试,但认为它不工作,因为DGV是通过数据表填充。

For Each dr As DataRow In GetResults_Meth().Rows
  dgScanned.Rows.Insert(0, dr.ItemArray)
  Dim test As String = cbPanel.SelectedItem
  dgScanned.Columns(8).ToString().Replace(" ", test)
Next dr

编辑:很抱歉缺少信息。DGV中有9列,其中一列隐藏,8列由SQL查询填充:

Public Sub frmLASend_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dgScanned.ColumnCount = 9
        dgScanned.Columns(0).Name = "Request ID"
        dgScanned.Columns(1).Name = "GivenName"
        dgScanned.Columns(2).Name = "Surname"
        dgScanned.Columns(3).Name = "DOB"
        dgScanned.Columns(4).Name = "Sex"
        dgScanned.Columns(5).Name = "Specimen"
        dgScanned.Columns(6).Name = "Referral Date"
        dgScanned.Columns(7).Name = "IgnoreMe"
        dgScanned.Columns(7).Visible = False
        dgScanned.Columns(8).Name = "TestPanel"

        cbPanel.Items.Add("Amino Acids")
        cbPanel.Items.Add("MACS")
        cbPanel.Items.Add("Methylation")
        cbPanel.Items.Add("Organic Acids")
        cbPanel.SelectedItem = "Amino Acids"

每次条形码扫描仅从数据库中产生一个结果,因此仅向DGV添加一行。因此,操作员将扫描与任务相关的所有条形码,这些条形码将出现在DGV中。
SQL查询:

Private Function GetResults_Meth() As DataTable

        Dim dtResults_Meth As New DataTable
        Dim connString As String = "Data Source=NP-SQL03\SQL2019;Initial Catalog=asl;User Id=aslmanager;Password=!_asl1_!"

        Using conn As New SqlConnection(connString)
            Using cmd As New SqlCommand("
                SELECT DISTINCT requests.request_id AS 'Request ID', patients.first_name AS GivenName, patients.surname AS Surname,
                concat(patients.day_birth, '/', patients.month_birth, '/', patients.year_birth) AS DOB, patients.sex AS 'Sex',
                specimens.specimen AS 'Specimen', referral_details.referral_dt AS 'Referral Date', labs.lab_name
                
                FROM
                { oj (((((((asl.dbo.requests requests INNER JOIN asl.dbo.requests_services requests_services ON
                requests.request_id = requests_services.request_id)
                INNER JOIN asl.dbo.referral_details referral_details ON
                    requests.request_id = referral_details.referring_request_id)
                INNER JOIN asl.dbo.patients patients ON
                    requests.patient_id = patients.patient_id)
                INNER JOIN asl.dbo.specimens specimens ON
                    requests_services.specimen_id = specimens.specimen_id)
                INNER JOIN asl.dbo.referral_details_services referral_details_services ON
                    requests_services.request_id = referral_details_services.request_id AND
                requests_services.service_id = referral_details_services.service_id)
                
                INNER JOIN asl.dbo.labs labs ON referral_details.to_lab_id = labs.lab_id)
                INNER JOIN asl.dbo.services services ON referral_details_services.service_id = services.service_id)
                LEFT OUTER JOIN asl.dbo.addresses addresses ON labs.lab_address_id = addresses.address_id}

                WHERE
                    referral_details.referral_dt >= @StartDate AND
                    referral_details.referral_dt <= @EndDate AND
                    labs.lab_id = '82' AND
                    requests.request_id = @LabID AND
                    services.service_abbrev IN ('SAMe', 'SAHe', 'THF', 'Folinic', '5MTHF', 'GluthR', 'Gluthox')

                GROUP BY
                    requests.request_id, patients.first_name, patients.surname,
                    patients.month_birth, patients.day_birth, patients.year_birth,
                    patients.sex, services.service_abbrev, specimens.specimen,
                    referral_details.referral_dt, requests.collection_dt, labs.lab_name
                
                ORDER BY
                    labs.lab_name ASC,
                    requests.request_id ASC

                ", conn)
                cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = dateFrom.Value
                cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = DateTo.Value
                cmd.Parameters.Add("@LabID", SqlDbType.Int).Value = txtBarcode.Text
                cmd.CommandTimeout = 0

                Try
                    conn.Open()
                    Dim reader As SqlDataReader = cmd.ExecuteReader()
                    dtResults_Meth.Load(reader)


Process:
                    If dtResults_Meth.Rows.Count = 0 Then
                        MessageBox.Show("This Lab ID cannot be added to the manifest because it is not in the referrals table or is not being referred for this panel. Please resolve and try again.", "No Referral", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Else

                        Return dtResults_Meth

                    End If
                Catch Ex As Exception
                    MessageBox.Show("Unable to connect to SQL Server. Please check your network connection and if required, report the issue to IT." & vbCrLf & vbCrLf & Ex.Message, "No Connection!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End Using

        End Using

    End Function

触发(条形码后回车):

Private Sub txtBarcode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtBarcode.KeyDown
        If e.KeyCode = Keys.Enter Then
            If txtBarcode.Text.Length < 7 Or txtBarcode.Text.Length > 7 Then
                My.Computer.Audio.Play("specierror.wav")
                MessageBox.Show("Scanned Lab ID is invalid. Please try again.", "Invalid Lab ID", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                If cbPanel.SelectedItem = "Methylation" Then

                    If GetResults_Meth() Is Nothing Then
                    Else
                        For Each dr As DataRow In GetResults_Meth().Rows

                            'Ignore the below. Was tinkering
                            dgScanned.Rows.Insert(0, dr.ItemArray)
                            Dim test As String = cbPanel.SelectedItem
                            dgScanned.Columns(8).ToString().Replace(" ", test)

                        Next dr
                        txtBarcode.Text = ""
                        txtBarcode.Focus()
                    End If
                ElseIf cbPanel.SelectedItem = "Organic Acids" Then

                    If GetResults_OrgA() Is Nothing Then
                    Else
                        For Each dr As DataRow In GetResults_OrgA().Rows

                            dgScanned.Rows.Insert(0, dr.ItemArray)

                        Next dr
                        txtBarcode.Text = ""
                        txtBarcode.Focus()
                    End If
                ElseIf cbPanel.SelectedItem = "MACS" Then

                    If GetResults_MACS() Is Nothing Then
                    Else
                        For Each dr As DataRow In GetResults_MACS().Rows

                            dgScanned.Rows.Insert(0, dr.ItemArray)

                        Next dr
                        txtBarcode.Text = ""
                        txtBarcode.Focus()
                    End If
                End If
            End If
        End If

    End Sub
kkbh8khc

kkbh8khc1#

看起来问题的关键是你从DataTable填充网格,然后你从网格中取回一些数据并尝试更改,然后无法将结果放回网格中。显而易见的解决方案是修改DataTable中的数据,甚至首先将你想要的实际数据放回DataTable中。
我不知道GetResults_Meth是如何编写的,但有一种选择是将ComboBox值直接放入查询中,这样它就在一开始就被添加到DataTable中,例如。

Dim sql = "SELECT Column1, @Column2 AS Column2 FROM SomeTable"

'...

myCommand.Parameters.Add("@Column2", SqlDbType.VarChar, 50).Value = myComboBox.Text

ComboBox中的文本现在将出现在查询结果集的每一行中。
另一种选择是先修改DataRow中的数据,然后将DataRow中的数据添加到网格中。

For Each dr As DataRow In GetResults_Meth().Rows
    dr(8) = cbPanel.Text

    dgScanned.Rows.Insert(0, dr.ItemArray)
Next dr

但是根据您的图像,您似乎需要列索引7,而不是8。

相关问题