VB.NET SQL - Datagridvew计算相同的行

时间:2015-12-11 10:18:07

标签: sql vb.net visual-studio-2010 datagridview

您好我有以下内容填充datagridview表,它没有问题,但它返回的事实,相同的多个条目,我希望它计算数量并将它们加在一起。我该怎么做?

        connect()
        DataGridView_b1.AutoGenerateColumns = True

        cmd.CommandText = "SELECT pcb, faultcode, component, quantity, type FROM [sqlcnvfaultentry] WHERE date = '" & breakdown_date.Value & "' and line = '" & breakdown_combo_line.Text & "' and hour = '" & breakdown_combo_hour.Text & "' and type = 'Process'"

        Dim dataAdapter_b1 = New SqlDataAdapter(cmd.CommandText, con.ConnectionString)
        Dim table_b1 As New DataTable()
        table_b1.Locale = System.Globalization.CultureInfo.InvariantCulture
        dataAdapter_b1.Fill(table_b1)
        Me.breakdown_bindingsource_dpmop.DataSource = table_b1

        DataGridView_b1.DataSource = breakdown_bindingsource_dpmop
        DataGridView_b1.Columns(0).HeaderText = "PCB:"
        DataGridView_b1.Columns(1).HeaderText = "Fault Code:"
        DataGridView_b1.Columns(2).HeaderText = "Component:"
        DataGridView_b1.Columns(3).HeaderText = "Quantity:"
        DataGridView_b1.Columns(4).HeaderText = "Type:"

        breakdown_dataselected.Text = "Current Data Range: " & breakdown_date.Value.Date & " for Line " & breakdown_combo_line.Text & " at " & breakdown_combo_hour.Text
        Label_dpmo.Text = "DPMO(p) Data:"

        disconnect()

我附上了我想要瞄准的图片(底部图片),而顶部正是我的目标。

任何帮助都会很棒。

干杯, 皮特

Pic

1 个答案:

答案 0 :(得分:0)

你正在寻找group by clause

cmd.CommandText = "SELECT pcb, faultcode, component, 
                   SUM(quantity) as quantity, type 
                   FROM [sqlcnvfaultentry] WHERE ...
                   GROUP BY pcb, faultcode, component,  type"

另外,请注意sql注入。 Use parametrized query