将DataGridViewComboBoxColumn绑定到SQL数据源

时间:2019-06-21 17:48:54

标签: vb.net

我试图创建一个将“模板”数据添加到SQL Server的用户窗体,他们将这些数据拉回到窗体上的datagrid视图中,从而允许用户输入详细信息。这些详细信息在数据库中具有约束,因此我需要组合框列来验证网格中的条目。目前,我已经建立了datagrid视图,它检索数据并添加combobox列,但是我不确定将详细信息发送回服务器的最佳方法是什么。如果可能的话,我想将组合框列绑定到数据源。对于如何最好地进行的任何建议将不胜感激。我将在下面的代码中添加代码。

        SQLCon.Open()
        'Form allows user to define a time frame 
        'spEmployeePoints takes parameters from form and adds the needed fields to the DB
        Dim sqlcmd As New SqlCommand("spEmployeePoints", SQLCon)
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.AddWithValue("@wkStart", dtpWeekStart.Value.ToString("MM-dd-yyyy"))
        sqlcmd.Parameters.AddWithValue("@wkID", wkID)

        sqlcmd.ExecuteNonQuery()

        'spEmployeePoints_DGV_LOAD retreives ROWID, Employee_ID, and Name from the DB and passes them to the datagridview
        Dim sqlcmd2 As New SqlCommand("spEmployeePoints_DGV_LOAD", SQLCon)
        sqlcmd2.CommandType = CommandType.StoredProcedure
        sqlcmd2.Parameters.AddWithValue("@wkStart", dtpWeekStart.Value.ToString("MM-dd-yyyy"))
        sqlcmd2.Parameters.AddWithValue("@wkID", wkID)

        Dim da As New SqlDataAdapter
        Dim ds = New DataSet()
        da.SelectCommand = sqlcmd2
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)

        'Adding DataGridViewComboBoxColumn that corospond with DB columns not brought in by spEmployeePoints_DGV_LOAD
        '  Need comboboxes so the form validation will match the CHECK constraint in the DB
        DataGridView1.Columns.Insert(3, cbMon)
        DataGridView1.Columns.Insert(4, cbTue)
        DataGridView1.Columns.Insert(5, cbWed)
        DataGridView1.Columns.Insert(6, cbThu)
        DataGridView1.Columns.Insert(7, cbFri)
        DataGridView1.Columns.Insert(8, cbSat)
        DataGridView1.Columns.Insert(9, cbSun)
        DataGridView1.Columns.Insert(10, cbBonus)

        'The form is structured the way tha I intend, now is there a way to bind the combobox columns back to the DB,
        '   something like, bind combobox column where combobox rowid = db rowid

因此,在用户创建模板之后,我希望将详细信息传输回服务器,但是我只能弄清楚如何将组合框列添加为添加的列,而不是作为已加载的sql数据的一部分

1 个答案:

答案 0 :(得分:0)

从SQL Server选择进入DataGridView。

Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    connetionString = "Data Source=Your_Server_Name_Here;Initial Catalog=Test;Trusted_Connection=True;"
    connection = New SqlConnection(connetionString)
    sql = "Select * from Orders"
    Try
        connection.Open()
        adapter = New SqlDataAdapter(Sql, connection)
        adapter.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        connection.Close()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

或者...

Imports System.Data.SqlClient
Public Class Form1
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim cmdBuilder As SqlCommandBuilder
    Dim ds As New DataSet
    Dim changes As DataSet
    Dim sql As String
    Dim i As Int32

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        connection = New SqlConnection(connetionString)
        Sql = "select * from Product"
        Try
            connection.Open()
            adapter = New SqlDataAdapter(Sql, connection)
            adapter.Fill(ds)
            connection.Close()
            DataGridView1.Data Source= ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            cmdBuilder = New SqlCommandBuilder(adapter)
            changes = ds.GetChanges()
            If changes IsNot Nothing Then
                adapter.Update(changes)
            End If
            MsgBox("Changes Done")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

http://vb.net-informations.com/dataadapter/dataadapter-datagridview-sqlserver.htm