VB2005中的Datagridview操作小技巧

ping502 2008-04-20 10:56:42
在這個版裡發覺很多人提關於Datagridview操作方面的問題。
我把我做系統中用到的操作小技巧發出來,
如有更好的方法或者我的有錯請指正。謝謝。

'指定列的事件
'自定議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
...全文
327 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
appleller 2009-11-19
  • 打赏
  • 举报
回复
.
ping502 2008-04-26
  • 打赏
  • 举报
回复
謝謝樓上各位兄弟的支持。
_NET2004 2008-04-23
  • 打赏
  • 举报
回复

'增加序列号
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


这个是在左边列增加序号
qinboody 2008-04-23
  • 打赏
  • 举报
回复
' 增加序列号
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
firein 2008-04-22
  • 打赏
  • 举报
回复
不错
jn_rice 2008-04-22
  • 打赏
  • 举报
回复
说明一下RowHeaderWidth需要调整到合适宽度,否则显示不全
jn_rice 2008-04-22
  • 打赏
  • 举报
回复
支持一下楼主

'增加序列号,写成事件,在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

ping502 2008-04-20
  • 打赏
  • 举报
回复
大家要是有什麼好的技巧也貼上來學習一下。。
ping502 2008-04-20
  • 打赏
  • 举报
回复

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
ping502 2008-04-20
  • 打赏
  • 举报
回复

'儲存格框線樣式。
DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.SunkenVertical
'是否顯示資料列列首的資料列。
DataGridView1.ColumnHeadersVisible = True
'儲存格的文字內容所套用的字型
DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font(DataGridView1.Font, FontStyle.Bold)
'表示要如何開始編輯儲存格。
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
_NET2004 2008-04-20
  • 打赏
  • 举报
回复
学习一下,有用
ping502 2008-04-20
  • 打赏
  • 举报
回复

'設定焦點們置
Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(1).Cells(0)
'取得或設定值,表示要如何選取 System.Windows.Forms.DataGridView 的儲存格。
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'是否允許使用者同時選取一個以上的 System.Windows.Forms.DataGridView 儲存格、資料列或資料行。
DataGridView1.MultiSelect = False

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧