16,717
社区成员
发帖
与我相关
我的任务
分享
'指定列的事件
'自定議datagridview的單元格keypress事件,處理不能輸字符
Private Sub dgv_Dtl_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim cotrol As Control = DirectCast(e.Control, Control)
AddHandler cotrol.KeyPress, AddressOf myDGV_KeyPress
AddHandler cotrol.KeyDown, AddressOf myKeyDown
End Sub
Private Sub myKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'屏了左右箭頭
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then
e.Handled = True
End If
End Sub
Private Sub myDGV_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Me.DataGridView1.CurrentCell.ColumnIndex = 4 then
If Not Char.IsNumber(e.KeyChar) Then
e.Handled = True
End If
end if
end sub
'增加序列号
Private Sub dgvCustProd_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles dgvCustProd.RowPostPaint
Using b As SolidBrush = New SolidBrush(dgvCustProd.RowHeadersDefaultCellStyle.ForeColor)
e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), _
dgvCustProd.DefaultCellStyle.Font, _
b, _
e.RowBounds.Location.X + 5, _
e.RowBounds.Location.Y + 4)
End Using
End Sub
'增加序列号,写成事件,在datagridview数据变化的时候调用。
For i As Integer = 0 To Datagridview1.RowCount - 1
Datagridview1.Rows(i).HeaderCell.Value = (i + 1).ToString
Dim dvs As New DataGridViewCellStyle()
dvs.Alignment = DataGridViewContentAlignment.MiddleCenter
Datagridview1.Rows(i).HeaderCell.Style = dvs
Next i
Private Sub DgvFactory_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DgvFactory.CellMouseDoubleClick
If e.RowIndex >= 0 Then '區別雙點表頭跟單元格的
End If
End Sub
'儲存格框線樣式。
DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SunkenVertical
'是否顯示資料列列首的資料列。
DataGridView1.ColumnHeadersVisible = True
'儲存格的文字內容所套用的字型
DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font(DataGridView1.Font, FontStyle.Bold)
'表示要如何開始編輯儲存格。
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
'設定焦點們置
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(1).Cells(0)
'取得或設定值,表示要如何選取 System.Windows.Forms.DataGridView 的儲存格。
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'是否允許使用者同時選取一個以上的 System.Windows.Forms.DataGridView 儲存格、資料列或資料行。
DataGridView1.MultiSelect = False