No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Frm_01HF_finish_chem.vb 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. Imports System.Data.SqlClient
  2. Public Class Frm_01HF_finish_chem
  3. Dim cmd As New SqlCommand
  4. Dim da As New SqlDataAdapter
  5. Dim dt As New DataTable
  6. Dim sql As String
  7. Dim conn As New SqlConnection
  8. Private Sub Frm_01HF_finish_chem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  9. conn.ConnectionString = connstring
  10. conn.Open()
  11. sql = "select top(300) date as 日期 ,no as 单号,article as 品名,type as 分类 from ht_finish_chem group by date,no,article,type order by date desc"
  12. cmd.CommandText = sql
  13. cmd.Connection = conn
  14. da.SelectCommand = cmd
  15. da.Fill(dt)
  16. DataGridView1.DataSource = dt
  17. DataGridView1.RowTemplate.Height = 35
  18. DataGridView1.Columns(0).Width = 120
  19. DataGridView1.Columns(1).Width = 120
  20. DataGridView1.Columns(2).Width = 180
  21. DataGridView1.Columns(3).Width = 150
  22. ' DataGridView1.Columns(4).Width = 120
  23. ' 根据内容调整 DataGridView 大小
  24. DataGridView2.Width = DataGridView2.PreferredSize.Width
  25. ' DataGridView1.Width = DataGridView2.PreferredSize.Width
  26. 'DataGridView2.Height = DataGridView2.PreferredSize.Height
  27. 'DataGridView2.AutoSize = True
  28. End Sub
  29. Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
  30. sql = "SELECT TOP (100) HT_finish_chem.chem AS 品名, HT_Chem_Main.sys_name AS 系统品名, CAST(HT_finish_chem.percents AS numeric(10, 1)) AS 比例, HT_finish_chem.weight AS 重量, CASE WHEN chem = '水' THEN '水' ELSE type1 END AS 分类
  31. FROM HT_finish_chem LEFT OUTER JOIN
  32. HT_Chem_Main ON HT_finish_chem.chem = HT_Chem_Main.in_use
  33. where date='" & DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells("日期").Value & "'and HT_finish_chem.no= '" & DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells("单号").Value & "'
  34. ORDER BY HT_finish_chem.weight DESC"
  35. cmd.CommandText = sql
  36. cmd.Connection = conn
  37. da.SelectCommand = cmd
  38. Dim dt1 As New DataTable
  39. da.Fill(dt1)
  40. ' 确保 DataTable 已填充到 DataGridView 中
  41. Dim totalPercents As Double = 0
  42. Dim totalWeight As Double = 0
  43. ' 遍历 DataTable 中的所有行,累加比例和重量
  44. For Each row As DataRow In dt1.Rows
  45. totalPercents += Convert.ToDouble(row("比例"))
  46. totalWeight += Convert.ToDouble(row("重量"))
  47. Next
  48. ' 在 DataTable 中添加一行,显示合计
  49. Dim totalRow As DataRow = dt1.NewRow()
  50. totalRow("品名") = "合计"
  51. totalRow("比例") = totalPercents
  52. totalRow("重量") = totalWeight
  53. dt1.Rows.Add(totalRow)
  54. ' 刷新 DataGridView,显示新的合计行
  55. DataGridView2.DataSource = dt1
  56. ' 将最后一行设置为红色字体
  57. Dim lastRowIndex As Integer = DataGridView2.Rows.Count - 2
  58. With DataGridView2.Rows(lastRowIndex)
  59. .DefaultCellStyle.ForeColor = Color.Red ' 设置字体颜色为红色
  60. .DefaultCellStyle.Font = New Font("Microsoft YaHei", 10, FontStyle.Bold) ' 加粗字体
  61. .DefaultCellStyle.BackColor = Color.LightYellow ' 设置背景颜色为淡黄色
  62. .ReadOnly = True ' 禁止编辑合计行
  63. End With
  64. DataGridView2.Columns(1).Width = 120
  65. DataGridView2.Columns(2).Width = 70
  66. DataGridView2.Columns(3).Width = 70
  67. DataGridView2.Columns(4).Width = 120
  68. End Sub
  69. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'search
  70. sql = "select top(300) date as 日期 ,no as 单号,article as 品名,type as 分类 from ht_finish_chem where article like '%" & TextBox1.Text & "%' or type like '%" & TextBox1.Text & "%' group by date,no,article,type order by date desc"
  71. cmd.CommandText = sql
  72. cmd.Connection = conn
  73. da.SelectCommand = cmd
  74. dt = New DataTable
  75. da.Fill(dt)
  76. DataGridView1.DataSource = dt
  77. DataGridView1.RowTemplate.Height = 35
  78. DataGridView1.Columns(0).Width = 120
  79. DataGridView1.Columns(1).Width = 120
  80. DataGridView1.Columns(2).Width = 180
  81. DataGridView1.Columns(3).Width = 150
  82. End Sub
  83. End Class