填充Datagrid

时间:2014-02-14 00:05:53

标签: sql vb.net datagridview datagrid

我正在创建一个选美评分系统,

我有这个函数用于使用query填充我的datagridview的第一列:

  Public Sub searchContestant()
    If comboCategory.SelectedIndex <> 0 Then
        Dim id As Integer = Login.JudgeBindingSource1.Current("Cont_id")
        Dim critID As Integer = Convert.ToInt32(lblID.Text)
        'search the contest record by the contest_id
        Dim con As New SqlConnection
        Dim reader As SqlDataReader
        Dim reader2 As SqlDataReader
        Dim dt = New DataTable()
        Dim nc As New DataGridViewTextBoxColumn

        DataGridView1.Columns.Clear()
        DataGridView1.Rows.Clear()

        Try
            con.ConnectionString = "Data Source=BROWNIE\SQLEXPRESS;Initial Catalog=PSS;Integrated Security=True"

            Dim cmd As New SqlCommand("SELECT * from Criteria where Cat_id = '" & critID & "' ", con)
            Dim cmd2 As New SqlCommand("SELECT * from Contestant where Cont_id = '" & id.ToString() & "' ", con)
            con.Open()

            ' Execute Query  
            reader = cmd.ExecuteReader()

            nc.Name = "Contestants"
            nc.Width = 250
            nc.ReadOnly = True
            DataGridView1.Columns.Add(nc)

            While reader.Read()
                nc = New DataGridViewTextBoxColumn
                nc.Name = "(" & reader.GetString(2) & "%)" & reader.GetString(1)
                nc.HeaderText = "(" & reader.GetString(2) & "%)" & reader.GetString(1)
                nc.Width = 150
                DataGridView1.Columns.Add(nc)
            End While

        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.    '
        End Try

        Try

            'populate contestant rows
            con.ConnectionString = "Data Source=BROWNIE\SQLEXPRESS;Initial Catalog=PSS;Integrated Security=True"

            Dim cmd2 As New SqlCommand("SELECT * from Contestant where Cont_id = '" & id.ToString() & "' ", con)
            con.Open()
            reader2 = cmd2.ExecuteReader()

            While reader2.Read()
                nc = New DataGridViewTextBoxColumn
                nc.Width = 100
                DataGridView1.Rows.Add(reader2.GetString(2).ToString())
            End While
        Catch ex As Exception
            MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
        Finally
            con.Close() 'Whether there is error or not. Close the connection.    '
        End Try

    End If
End Sub

接下来的专栏:

Public Sub searchCriteria()
    Dim con As New SqlConnection
    Dim reader4 As SqlDataReader
    Dim dt = New DataTable()
    Dim nc As New DataGridViewTextBoxColumn

    DataGridView1.Columns.Clear()

    Try

        'populate Criteria
        con.ConnectionString = "Data Source=BROWNIE\SQLEXPRESS;Initial Catalog=PSS;Integrated Security=True"
        Dim cmd As New SqlCommand("SELECT * from Criteria where Cat_id = '" & lblID.Text & "' ", con)
        con.Open()

        reader4 = cmd.ExecuteReader
        While reader4.Read
            nc = New DataGridViewTextBoxColumn
            nc.Name = reader4.GetString(1).ToString & Chr(13) & reader4.GetString(2).ToString & "%"

            nc.Width = 100
            DataGridView1.Columns.Add(nc)
        End While

    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
    Finally
        con.Close() 'Whether there is error or not. Close the connection.    '
    End Try

End Sub

这是输出:

enter image description here

每次法官登录时,我都会使用此功能将数据插入系统:

Public Sub makeTransaction()
    Dim con2 As New SqlConnection

    'get the judge id upon logging in
    Dim judgeID = Login.JudgeBindingSource1.Current("Judge_id")

    'get the contest id of the judge
    Me.JudgeTableAdapter.FillByJudgeID(Me.PSSDataSet.Judge, judgeID)
    Dim JudgeContestid = Login.JudgeBindingSource1.Current("Cont_id")
    Me.ContestantTableAdapter.FillByContestID(Me.PSSDataSet.Contestant, JudgeContestid)

    'get the contestant id
    Me.ContestTableAdapter.FillByContestID(Me.PSSDataSet.Contest, JudgeContestid)
    Dim contestID As Integer = ContestBindingSource.Current("Cont_id")

    'get the category of the contest

    Me.CategoryTableAdapter.FillByContestID(Me.PSSDataSet.Category, Convert.ToInt32(contestID))
    For Each Category As DataRow In Me.PSSDataSet.Category.Rows
        Me.CriteriaTableAdapter.FillByCategoryID(Me.PSSDataSet.Criteria, Category("Cat_id"))
        For Each Contestant As DataRow In Me.PSSDataSet.Contestant.Rows
            For Each Criteria As DataRow In Me.PSSDataSet.Criteria.Rows
                TransactionsTableAdapter.Insert(JudgeContestid, Contestant("Con_id"), Criteria("Cri_id"), 0)
            Next
        Next
    Next


End Sub

现在问题是,如何使用MakeTransaction()函数最近生成的数据填充数据网格?

0 个答案:

没有答案