PDA

View Full Version : سوال: محدود کردن یک سلول در دیتاگرید



کاکرودی
سه شنبه 06 اسفند 1398, 21:44 عصر
با سلام
دیتا گریدی دارم در برنامه که بعضی از سلولها فقط باید عدد دریافت کنند چه کدی باید بنویسم و در چه رویدادی ؟؟

the king
سه شنبه 06 اسفند 1398, 22:26 عصر
با سلام
دیتا گریدی دارم در برنامه که بعضی از سلولها فقط باید عدد دریافت کنند چه کدی باید بنویسم و در چه رویدادی ؟؟
اگر می خواهید کاربر هر چیزی که دلش میخواد تایپ کنه، اما فقط اعداد قابل تایید باشند (Validate بشن) :

Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Try
If ((e.RowIndex <> DataGridView1.NewRowIndex) OrElse (String.IsNullOrEmpty(e.FormattedValue?.ToString() ) = False)) Then
Convert.ToInt32(e.FormattedValue)
End If
Catch
If DataGridView1.EditingControl IsNot Nothing Then
DataGridView1.EditingControl.BackColor = Color.Yellow
End If
e.Cancel = True
End Try
End Sub

اما اگر میخواهید اصلا نتونه کاراکتر های نامعتبر مثل حروف رو تایپ کنه :

Private Sub editingControl_KeyPress(sender As Object, e As KeyPressEventArgs)
If ((Char.IsControl(e.KeyChar) = False) AndAlso (Char.IsDigit(e.KeyChar) = False)) Then
e.Handled = True
End If
End Sub

Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
RemoveHandler DataGridView1.EditingControl.KeyPress, AddressOf editingControl_KeyPress
AddHandler DataGridView1.EditingControl.KeyPress, AddressOf editingControl_KeyPress
End Sub


طبعا اگر قراره برای سطر و ستون های خاصی اعمال بشه اون رو هم با if مشخص می کنید.

کاکرودی
چهارشنبه 07 اسفند 1398, 00:04 صبح
ممنونم دوست عزیز