显示行数和列数

时间:2018-06-10 22:14:50

标签: vb.net

我在我的表MS Access中命名了(Table1)两个字段(ID1 - Team1)。 使用NumericUpDown1我选择在DataGridView2中随机化后要显示的行数。使用NumericUpDown2我选择在DataGridView2中随机化后要显示的列数。如果我选​​择NumericUpDown2只有一列(数字1)它与此查询一起工作得非常好:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Con_randomize()
    Dim rows As Integer
    If Not Integer.TryParse(NumericUpDown1.Value, rows) Then
        MsgBox("NUMBER NOT AVAILABLE", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
        NumericUpDown1.Value = ""
        NumericUpDown1.Focus()
        Exit Sub
    End If
    If NumericUpDown2.Value = 1 Then
        Dim sql As String = String.Format("SELECT Top {0} ID1,Team1 From Table1 ORDER BY RND(-(100000*ID1)*Time())", rows)
        InfoCommand = New OleDbCommand(sql, Con_randomize)
        InfoAdapter = New OleDbDataAdapter()
        InfoAdapter.SelectCommand = InfoCommand
        InfoTable = New DataTable()
        InfoAdapter.Fill(InfoTable)
        DataGridView2.DataSource = InfoTable
        DataGridView2.Columns(0).HeaderText = "NUMERO"
        DataGridView2.Columns(1).HeaderText = "CATEGORY1"
    End If
End Sub

如果我在NumericUpDown2中选择要在Datagridview2中显示的数字2或3列,如何制作。 列将被命名(CATEGORY2 - CATEGORY3)。例如(1 Victor - David - Vincent)(2 wiliam- George - Joseph)..在我唯一一个名为Team1的领域我有一百个名字

1 个答案:

答案 0 :(得分:0)

我不是百分之百确定为什么你需要一个查询来初始化数据,但是一试。它会以您需要的方式自动创建列。

' If NumericUpDown2.Value = 1 Then ' Comment This If Block Out

' Create the Teams String
Dim teamsString as New System.Text.StringBuilder("")
For i as Integer = 1 to Convert.ToInt32(NumericUpDown2.Value)
    teamsString.Append(", (SELECT Top 1 Team1 From Table1 ORDER BY RND(-(100000*ID1)*Time())) as Category" + i.ToString())
Next

Dim sql As String = String.Format("SELECT Top {0} ID1" + teamsString.ToString() + " From Table1 ORDER BY RND(-(100000*ID1)*Time())", rows)
InfoCommand = New OleDbCommand(sql, Con_randomize)
InfoAdapter = New OleDbDataAdapter()
InfoAdapter.SelectCommand = InfoCommand
InfoTable = New DataTable()
InfoAdapter.Fill(InfoTable)
DataGridView2.DataSource = InfoTable
DataGridView2.Columns(0).HeaderText = "NUMERO"

' Don't Need This, We Made It the Binding Name
' DataGridView2.Columns(1).HeaderText = "CATEGORY1"