Author Topic: DataGridView 相關的程序函數庫  (Read 3454 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
DataGridView 相關的程序函數庫
« on: June 12, 2012, 02:01:20 AM »
停止使用欄位排序的功能
Code: [Select]
Public Sub DisableDataGridViewSorting(ByRef tmpDataGridView As DataGridView)
        For Each dgvCol As DataGridViewColumn In tmpDataGridView.Columns
            dgvCol.SortMode = DataGridViewColumnSortMode.NotSortable
        Next

End Sub

設定欄位排序的方式
Code: [Select]
Public Sub SetDataGridViewColumnSorting(ByRef tmpDataGridView As DataGridView, tmpColumn As Integer, tmpSortMode As DataGridViewColumnSortMode)
        If tmpColumn <= tmpDataGridView.Columns.Count - 1 Then
            tmpDataGridView.Columns(tmpColumn).SortMode = tmpSortMode
        End If
End Sub

加入Checkbox 類別的欄位
Code: [Select]
Public Sub AddDataGridViewCheckBoxColumn(ByRef tmpDataGridView As DataGridView, Optional ByVal tmpColPos As Integer = -1, Optional ByVal tmpHeaderText As String = "", Optional ByVal tmpWidth As Integer = 30)

        Dim NewColumn As New DataGridViewCheckBoxColumn
        NewColumn.HeaderText = tmpHeaderText
        NewColumn.Width = tmpWidth
        NewColumn.TrueValue = 1
        NewColumn.FalseValue = 0
        NewColumn.InheritedStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        If tmpColPos = -1 Or tmpColPos > tmpDataGridView.ColumnCount Then
            tmpDataGridView.Columns.Add(NewColumn)
        Else
            tmpDataGridView.Columns.Insert(tmpColPos, NewColumn)

        End If

End Sub

加入Combobox 類別的欄位
Code: [Select]
Public Sub AddDataGridViewComboBoxColumn(ByRef tmpDataGridView As DataGridView, ByVal tmpData As Object, Optional ByVal tmpColPos As Integer = -1, Optional ByVal tmpHeaderText As String = "", Optional ByVal tmpWidth As Integer = 30)
        Dim comboboxColumn As DataGridViewComboBoxColumn
        comboboxColumn = CreateComboBoxColumn(tmpHeaderText, tmpWidth)
        FillDataGridViewComboBoxColumn(comboboxColumn, tmpData)

        If tmpColPos = -1 Or tmpColPos > tmpDataGridView.ColumnCount Then
            tmpDataGridView.Columns.Add(comboboxColumn)
        Else
            tmpDataGridView.Columns.Insert(tmpColPos, comboboxColumn)
        End If

End Sub

Private Function CreateComboBoxColumn(Optional ByVal tmpHeaderText As String = "", Optional ByVal tmpWidth As Integer = 30) As DataGridViewComboBoxColumn
        Dim column As New DataGridViewComboBoxColumn()
        With column
            .HeaderText = tmpHeaderText
            .DropDownWidth = 160
            .Width = tmpWidth
            .MaxDropDownItems = 6
            .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
            .FlatStyle = FlatStyle.Standard

        End With
        Return column

End Function

Private Sub FillDataGridViewComboBoxColumn(ByRef tmpComboBoxColumn As DataGridViewComboBoxColumn, ByVal tmpData As Object)
        Dim tmpDataTable As New DataTable
        Dim intY As Integer
        Select Case VarType(tmpData)
            Case VariantType.String
                If Len(tmpData) > 0 Then
                    If CreateDBReader(tmpData, grsOpen) = True Then
                        tmpComboBoxColumn.Items.Clear()
                        tmpDataTable.Columns.Add("DisplayData")
                        tmpDataTable.Columns.Add("ItemData")
                        While grsOpen.Read
                            tmpDataTable.Rows.Add(New Object() {grsOpen(0), grsOpen(1)})

                        End While
                        grsOpen.close()
                        tmpComboBoxColumn.DataSource = tmpDataTable
                        tmpComboBoxColumn.DataPropertyName = "itemdata"
                        tmpComboBoxColumn.DisplayMember = "displaydata"
                        tmpComboBoxColumn.ValueMember = "itemdata"
                    End If
                End If

            Case VariantType.Array + VariantType.String
                If Not IsNothing(tmpData) Then
                    tmpComboBoxColumn.Items.Clear()
                    tmpDataTable.Columns.Add("DisplayData")
                    tmpDataTable.Columns.Add("ItemData")
                    For intY = 0 To UBound(tmpData, 2)
                        tmpDataTable.Rows.Add(New Object() {tmpData(0, intY), tmpData(1, intY)})
                    Next
                    tmpComboBoxColumn.DataSource = tmpDataTable
                    tmpComboBoxColumn.DataPropertyName = "itemdata"
                    tmpComboBoxColumn.DisplayMember = "displaydata"
                    tmpComboBoxColumn.ValueMember = "itemdata"

                End If
        End Select
End Sub

設定欄位表頭文字的對齊方式
Code: [Select]
Public Sub SetDataGridViewHeaderAlignment(ByRef tmpDataGridView As DataGridView, ByVal tmpAlignment As DataGridViewContentAlignment)
        Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
        dgvColumnHeaderStyle.Alignment = tmpAlignment

        tmpDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle

End Sub
« Last Edit: June 17, 2012, 02:17:06 AM by Roy Chan »