VB.net 中的 DataGridView 操作

问题描述 投票:0回答:1

这是我第一次使用 vb,我必须制作一个包含 10 列和可变行数的网格,并在单元格上显示数据。

 DataGridView1.RowHeadersVisible = True


 Dim numColumns As Integer
 If Integer.TryParse(word.Text, numColumns) AndAlso numColumns > 0 Then
     DataGridView1.Columns.Clear()
     For i As Integer = 1 To 10
         Dim newColumn As New DataGridViewTextBoxColumn()
         newColumn.HeaderText = i - 1
         newColumn.ReadOnly = True
         DataGridView1.Columns.Add(newColumn)
     Next
     For i As Integer = 0 To numColumns - 1
         If i Mod 10 = 0 AndAlso i > 0 Then
             If DataGridView1.RowCount <= i \ 10 Then
                 DataGridView1.Rows.Add()
             End If
         End If
         If DataGridView1.RowCount > i \ 10 Then
             Dim index As Integer = i Mod 10
             If index >= 0 And index < DataGridView1.ColumnCount Then
                 DataGridView1.Rows(i \ 10).Cells(index).Value = i
             End If
         End If
     Next
 Else
     MsgBox("Insira um valor valido e positivo para o número de DM's")
 End If

我尝试了不同的号码和一些chatgpt/blackbox,但没有解决任何问题

vb.net datagridview
1个回答
0
投票

这个答案不使用数据绑定到数据网格视图 如果你问为什么,因为我可以更好地控制 DGV 的外观 这两个函数在表单加载 StyleDGV 和 ViewSearches 时被调用

 Private Sub frmViewTX_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    StyleDGV()
    ViewSearches()

End Sub

    Private Sub StyleDGV()

    'Set Design of the DataGridView
    dgvTX.DefaultCellStyle.Font = New Font("Times New Roman", 13.0F, FontStyle.Bold)
    dgvTX.ColumnCount = 8
    dgvTX.Columns(0).Width = 50   'ID
    dgvTX.Columns(1).Width = 140  'Date
    dgvTX.Columns(2).Width = 136  'TxType
    dgvTX.Columns(3).Width = 120  'Amt
    dgvTX.Columns(4).Width = 114  'ckNum
    dgvTX.Columns(5).Width = 390  'Desc
    dgvTX.Columns(6).Width = 130  'Bal
    dgvTX.Columns(7).Width = 10  'dumbS
    'To Set Col Header Size Mode = Enabled
    'To Set Col Header Default Cell Styles DO in Properties
    dgvTX.Columns(6).DefaultCellStyle.Format = "c"


    dgvTX.ColumnHeadersHeight = 10 'Sans Serif 'Tahoma
    dgvTX.ColumnHeadersDefaultCellStyle.Font = New Font("Sans Serif", 12.0F, FontStyle.Bold)

    dgvTX.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue
    dgvTX.DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
    dgvTX.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    dgvTX.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
    dgvTX.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    dgvTX.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight


    'dgvTX.Enabled = False 'Prevents Selection of data and scrolling
    'DGV Header Names
    dgvTX.Columns(0).Name = "TID"
    dgvTX.Columns(1).Name = "  Date"
    dgvTX.Columns(2).Name = "  Tx Type"
    dgvTX.Columns(3).Name = "  Amount"
    dgvTX.Columns(4).Name = "  CK Num"
    dgvTX.Columns(5).Name = "Desctiption"
    dgvTX.Columns(6).Name = "  Balance"
    dgvTX.Columns(7).Name = "     "


    dgvTX.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
    dgvTX.Columns(1).SortMode = DataGridViewColumnSortMode.NotSortable
    dgvTX.Columns(2).SortMode = DataGridViewColumnSortMode.NotSortable
    dgvTX.Columns(3).SortMode = DataGridViewColumnSortMode.NotSortable
    dgvTX.Columns(4).SortMode = DataGridViewColumnSortMode.NotSortable
    dgvTX.Columns(5).SortMode = DataGridViewColumnSortMode.NotSortable
    dgvTX.Columns(6).SortMode = DataGridViewColumnSortMode.NotSortable

End Sub

Private Sub ViewSearches()

    Dim intID As Integer
    Dim strDate As String
    Dim strTxType As String
    Dim strAmt As Decimal
    Dim strCKNum As String
    Dim strDesc As String
    Dim strBal As Decimal
    Dim rowCount As Integer
    Dim maxRowCount As Integer
    Dim emptyStr As String = "  "

    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
        conn.Open()

        Using cmd As New SQLiteCommand("", conn)

            If gvSearchType = "All" Then
                cmd.CommandText = "SELECT * FROM TxData"
            ElseIf gvSearchType = "MoRangeYr" Then
                cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth >= $gvFromMonth AND txSearchMonth <= $gvToMonth AND txYear = $gvYear "
                'cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth BETWEEN $gvFromMonth AND $gvToMonth AND txYear = $gvYear "
                cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
                cmd.Parameters.AddWithValue("$gvToMonth", gvToMonth)
                cmd.Parameters.AddWithValue("$gvYear", gvYear)
            ElseIf gvSearchType = "MoYr" Then
                cmd.CommandText = "SELECT * FROM TxData WHERE txSearchMonth = $gvFromMonth AND txYear = $gvYear "
                cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
                cmd.Parameters.AddWithValue("$gvYear", gvYear)
            ElseIf gvSearchType = "TxMoYr" Then
                cmd.CommandText = "SELECT * FROM TxData WHERE txType = $gvTxType AND txSearchMonth = $gvFromMonth AND txYear = $gvYear "
                cmd.Parameters.AddWithValue("$gvTxType", gvTxType)
                cmd.Parameters.AddWithValue("$gvFromMonth", gvFromMonth)
                cmd.Parameters.AddWithValue("$gvYear", gvYear)

            ElseIf gvSearchType = "Year" Then
                cmd.CommandText = "SELECT * FROM TxData WHERE txYear = $gvYear"
                cmd.Parameters.AddWithValue("$gvYear", gvYear)
            End If


            Using rdr As SQLite.SQLiteDataReader = cmd.ExecuteReader
                'dgvTX.DataSource = rdr
                'Statement Above is when DB is bound to dgvTX
                '============================================

                While rdr.Read()
                    intID = CInt((rdr("TID")))
                    strDate = rdr("txSortDate").ToString 'txSearchMonth '
                    strTxType = rdr("txType").ToString
                    strAmt = CDec(rdr("txAmount"))
                    strCKNum = rdr("txCKNum").ToString
                    strDesc = rdr("txDesc").ToString
                    strBal = CDec(rdr("txBalance"))
                    dgvTX.Columns(3).DefaultCellStyle.Format = "N"
                    dgvTX.Columns(6).DefaultCellStyle.Format = "N"
                    'dgvTX.Columns(6).DefaultCellStyle.Format = "C"'Adds the $ sign and commas
                    dgvTX.Rows.Add(intID, strDate, strTxType, strAmt, strCKNum, strDesc, strBal, emptyStr)

                    rowCount = rowCount + 1

                End While

                dgvTX.Columns(3).DefaultCellStyle.Format = "N"
                dgvTX.Columns(6).DefaultCellStyle.Format = "N"
                dgvTX.Sort(dgvTX.Columns(0), ListSortDirection.Descending)
            End Using

            If rowCount <= 25 Then
                maxRowCount = 25 - rowCount
                For iA = 1 To maxRowCount
                    dgvTX.Rows.Add(" ")
                Next
            End If


            lblAmt.Text = $"Current Balance  {Date.Today:MMMM d yyyy}"
            Dim dateToday = Date.Today
            Dim lblDate As String = dateToday.ToString("MMM" & " " & "d" & " " & "yyyy")
            lblAmt.Text = "Current Balance  " & lblDate

            GetCBBalance()

            tbBal.Text = "$ " & String.Format("{0:n}", strTotalBal)

        End Using
    End Using

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.