PDA

View Full Version : enter در سلول های datagridview



nazila_f
پنج شنبه 26 بهمن 1385, 11:33 صبح
من میخوام با زدن اینتر در سلول های datagridview به سلول بعدی بروم ولی event ای برای این کار وجود نداره و keydown فقط برای خود datagrid کار می کنه نه برای سلول هاش.از کدام event باید استفاده کنم .event cell enter را هم امتحان کردم ولی نشد.

sh
پنج شنبه 26 بهمن 1385, 12:13 عصر
To make a datagridview move to the next column when you press the enter you need to create a new control which inherits from datagridview. In the new control override PreProcessMessage to prevent the default enter key behavior. We also created a custom textbox column which converts an enter key press to tab key.
--------------------------------------------------------------------------------

Imports System.Data.SqlClient
Public Class Form1
Dim WithEvents dg As New DGV
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dg.Dock = DockStyle.Fill
Me.Controls.Add(dg)
Dim dt As New DataTable
Dim strConn As String = _
"Server = .;Database = NorthWind; Integrated Security = SSPI;"
Dim conn As New SqlConnection(strConn)
Dim da As New SqlDataAdapter("Select LastName, Notes from Employees", conn)
da.Fill(dt)
dg.DataSource = dt
dg.Columns.Remove("LastName")
dg.Columns.Remove("Notes")
Dim colName As New NoEnterColumn
With colName
.DataPropertyName = "LastName"
End With
Dim colNotes As New NoEnterColumn
With colNotes
.DataPropertyName = "Notes"
End With
dg.Columns.Add(colName)
dg.Columns.Add(colNotes)
End Sub
' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub CellKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = Chr(Keys.Enter) Then
e.Handled = True
dg.EndEdit()
End If
End Sub

Private Sub dg_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlSho wingEventArgs) Handles dg.EditingControlShowing
If Not TypeOf e.Control Is TextBox Then Return
Dim tb As TextBox = CType(e.Control, TextBox)
If Not tb Is Nothing Then
tb.AcceptsReturn = False
End If
End Sub
End Class

Public Class DGV
Inherits DataGridView
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_CHAR As Integer = &H102
Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(msg.WParam.ToInt32(), Keys) And Keys.KeyCode
' for a datagrid, we need to eat the tab key oe else its done twice
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter Then
Dim intRow As Integer = Me.CurrentCell.RowIndex
Dim intCol As Integer = Me.CurrentCell.ColumnIndex
intCol += 1
If intCol = Me.ColumnCount Then
intCol = 0
intRow += 1
If intRow = Me.RowCount Then
intRow = 0
End If
End If
Me.CurrentCell = Me(intCol, intRow)
Return True
End If
Return MyBase.PreProcessMessage(msg)
End Function
End Class
Public Class NoEnterColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New NoEnterCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a CalendarCell.
If Not (value Is Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(NoEnterCe ll)) _
Then
Throw New InvalidCastException("Must be a NoEnterCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class NoEnterCell
Inherits DataGridViewTextBoxCell
Public Sub New()
' Use the short date format.
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl As NoEnterEditingControl = _
CType(DataGridView.EditingControl, NoEnterEditingControl)
ctl.Text = Me.Value.ToString
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that CalendarCell uses.
Return GetType(NoEnterEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that CalendarCell contains.
Return GetType(String)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return ""
End Get
End Property
End Class
Class NoEnterEditingControl
Inherits NoEnterTB
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormatte dValue
Get
Return Me.Text
End Get
Set(ByVal value As Object)
If TypeOf value Is [String] Then
Me.Text = CStr(value)
End If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlForma ttedValue
Return Me.Text
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditin gControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInp utKey
Return True
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlF orEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingContr olOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGrid View
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueCha nged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDir ty(True)
MyBase.OnTextChanged(e)
End Sub
End Class
Public Class NoEnterTB
Inherits TextBox
Private Const WM_KEYDOWN As Integer = &H100
Private Const WM_KEYUP As Integer = &H101
Private Const WM_CHAR As Integer = &H102

Public Overrides Function PreProcessMessage(ByRef msg As System.Windows.Forms.Message) As Boolean
Dim keyCode As Keys = CType(msg.WParam.ToInt32(), Keys) And Keys.KeyCode
If msg.Msg = WM_KEYDOWN AndAlso keyCode = Keys.Enter Then
msg.WParam = Keys.Tab
End If
Return MyBase.PreProcessMessage(msg)
End Function
End Class

nazila_f
یک شنبه 29 بهمن 1385, 11:08 صبح
من از کد شما استفاده کردم.ممنون.خیلی خوب بود

فقط مشکلی که دارم اینه که یه event با نام Cellkeypress تعریف کرده بودید ولی عملا کار نمی کنه .مثلا من می خوام دو ستون فقط عدد بگیره و فرمت اون برای پول 3 تا 3 تا جدا شه که این کد رو باید در event Cellkeypress بنویسم .چه کار باید بکنم ؟

programmermp
یک شنبه 29 بهمن 1385, 12:44 عصر
کسی می دونه ؟

سلام

ببین می تونی تو ی این قسمت که در عکس گذاشتم کاری بکنی یا نه من که هر کاری

کردم جواب نداد روی دیتاگراید ویو گزینه edit column رو انتخاب کن و بعد

مانند عکس های زیر مسیر رو طی کنی

nazila_f
یک شنبه 29 بهمن 1385, 12:49 عصر
تمام این تنظیمات رو هم انجام دادم ولی جواب نمیده:عصبانی++:

Neda_Bagheri
دوشنبه 30 بهمن 1385, 11:27 صبح
منم همین مشکل رو دارم لطفا یکی جواب بده

sh
دوشنبه 30 بهمن 1385, 17:10 عصر
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, _
ByVal e As DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing
Dim txtEdit As TextBox = e.Control
'remove any existing handler
RemoveHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
AddHandler txtEdit.KeyPress, AddressOf txtEdit_Keypress
End Sub
Private Sub txtEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Console.WriteLine("KeyPress " & e.KeyChar.ToString())
'Test for numeric value or backspace in first column
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
If IsNumeric(e.KeyChar.ToString()) _
Or e.KeyChar = ChrW(Keys.Back) _
Or e.KeyChar = "." Then
Console.WriteLine("KeyPress number")
e.Handled = False 'if numeric display
Else
Console.WriteLine("Enter Numbers Only")
e.Handled = True 'if non numeric don't display
End If
End If
End Sub

nazila_f
پنج شنبه 03 اسفند 1385, 13:44 عصر
ممنون آقای شهریار.درست شد. ولی فرمتش که 3 تا 3 تا جدا کنه رو پیدا نکردم اگه ممکنه یه کم در این مورد راهنماییم کنید

programmermp
پنج شنبه 03 اسفند 1385, 14:01 عصر
یه مثال واست می زارم خوب دقت کن به کدهاش

ستون اخریش همونیه که می خواهی

اصل مطلب بر می گرده به این کد زیر که سه سه تا جدا می کنه :




DataGridView1.Columns("asdadad").DefaultCellStyle.Format = "n"


فرمت ستون رو از نوع عدد می کنه که سه تا سه تا جدا میشن

فکر کنم همین رو می خواستی

موفق باشید

nazila_f
شنبه 05 اسفند 1385, 10:35 صبح
ممنون . مشکلم حل شد . حالا می خوام دو تا از سلول های گریدم(دو ستون اول) اصلا Focous روش نره نه با Enter و نه با Tab . یه کد نوشتم که وقتی روی دو ستون اول رفت CurrentCell رو ستون سومم بذاره ولی چون در Enter نوشتم (کد بالا) بعد از زدن دو Enter کار می کنه؟ کجا باید این کارو بکنم؟ کدی وجود داره که Tabindex دو ستون از ستون های گرید False بشه ؟

آینار-آینار
چهارشنبه 02 خرداد 1386, 10:07 صبح
سلام
من از این کد استفاده کردم اما مشکلی که دارم اینه که دائما فوکوس باید بین یک textbox وگرید جابه جا بشه یعنی ببه اخرین ستون یک سطر که رسیدیم باید فوکوس به textbox داده بشه و همینجور جابه جا بشه
چی کار باید بکنم با این کد با اینتر به آخر ستون می رسم اما با جابه جا کردن فوکوس ارور می ده و کار نمی کنه