DataGridView添加行

时间:2018-12-30 23:35:33

标签: vb.net datagridview

enter image description here我试图将行添加到我想要的DataGridView中,但是在进行一些数学运算的地方我没有边界。
我想要它下面的那列的总和。

注意:Cell(10)是无界的。在这种情况下,我得到一个错误:

  

列不存在

(显然,它不在数据表中)。这是我的代码:

Public Sub PrintData()
    PrintSQL.ExecQuery("SELECT * FROM Cisnik WHERE Datum = '" & 
                       CDate(LB2.Text).ToString("MM.dd.yyyy") & "'; ")
    If PrintSQL.HasException(True) Then Exit Sub
    DGV3Print.DataSource = PrintSQL.DBDT

    For Each r As DataGridViewRow In DGV3Print.Rows
        r.Cells(10).Value = (r.Cells(1).Value - r.Cells(2).Value - r.Cells(4).Value - r.Cells(6).Value)
        tb = tb + r.Cells(10).Value
    Next

    Dim myrow = PrintSQL.DBDT.NewRow
    myrow(0) = "CELKEM"
    myrow(10) = tb
    PrintSQL.DBDT.Rows.Add(myrow)
End Sub

我将不胜感激

4 个答案:

答案 0 :(得分:0)

您的Datum列是否真的以字符串形式存储在数据库中?您实际上不应该串联字符串来构建SQL查询。学习使用参数。为什么要使用字符串,将其转换为日期,然后再转换为字符串?要解决您的问题,只需让数据库完成工作即可。这只是演示了要使用的模式。然后绑定到您的网格。

Dim s = "Select Num1, Num2, Num3, Num1 + Num2 + Num3 As Total From Numbers;"

答案 1 :(得分:0)

我是这样解决的:可能是业余爱好者,因为我是个初学者,但实际上可以按我需要的方式工作。代码是:

 Dim myrow = PrintSQL.DBDT.NewRow
    Dim i As Integer
    i = DGV3Print.Rows.Count - 1
    DGV3Print.Rows(i).Cells(0).Value = "CELKEM"
    DGV3Print.Rows(i).Cells(10).Value = tb
    PrintSQL.DBDT.Rows.Add(myrow)

无论如何,谢谢您的建议

答案 2 :(得分:0)

将行添加到DataGridView的简单得多的方法是:

DataGridView.Rows.Add({Data1,Data2})

答案 3 :(得分:0)

要将行添加到DateGridView,请参见下面的示例。

Imports System.Data.SqlClient
Public Class Form1
    Dim sCommand As SqlCommand
    Dim sAdapter As SqlDataAdapter
    Dim sBuilder As SqlCommandBuilder
    Dim sDs As DataSet
    Dim sTable As DataTable

    Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
        Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
        Dim sql As String = "SELECT * FROM Stores"
        Dim connection As New SqlConnection(connectionString)
        connection.Open()
        sCommand = New SqlCommand(sql, connection)
        sAdapter = New SqlDataAdapter(sCommand)
        sBuilder = New SqlCommandBuilder(sAdapter)
        sDs = New DataSet()
        sAdapter.Fill(sDs, "Stores")
        sTable = sDs.Tables("Stores")
        connection.Close()
        DataGridView1.DataSource = sDs.Tables("Stores")
        DataGridView1.ReadOnly = True
        save_btn.Enabled = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    End Sub

    Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
        DataGridView1.[ReadOnly] = False
        save_btn.Enabled = True
        new_btn.Enabled = False
        delete_btn.Enabled = False
    End Sub

    Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click
        If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
            sAdapter.Update(sTable)
        End If
    End Sub

    Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
        sAdapter.Update(sTable)
        DataGridView1.[ReadOnly] = True
        save_btn.Enabled = False
        new_btn.Enabled = True
        delete_btn.Enabled = True
    End Sub
End Class

http://vb.net-informations.com/datagridview/vb.net_datagridview_database.htm

我知道您可以对DataGridView中的数据点求和,但是我已经很长时间没有这样做了。像这样的东西应该很接近...

Dim sum As Integer = 0
For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step +1
    sum = sum + dataGridView1.Rows(i).Cells(2).Value
Next

textBoxSum.Text = sum.ToString()