為WinForm DataGrid中的Cell加入Double Click事件!
前言
我們有個Win Form系統一登入後會顯示一個Todo List的Grid,允許讓使用者在Grid上Double Click後,串到該功能去! 但是發現Double Click Cell卻無法串過去? 原來在DataGrid的Cell中Double Click不會引發DataGrid的Double Click事件。
研究
直覺想說,應該要像Web一樣,按那裡都要可以串到功能去呀! 上網找到了「捕捉DataGrid的雙擊事件(C#版本)」,
原來變成取textbox的Double Click事件及在Grid MouseDown事件及Textbox MouseDown之間的時間是否算是Double Click的時間之內即可!
1.所以先定義一個全域變數gridMouseDownTime來記錄Grid MouseDown的時間。
2.定義2個函式(TextBoxDoubleClickHandler, TextBoxMouseDownHandler)來處理Cell的Double Click事件。
3.透過BindGridCellDBClick函式來設定DataGrid各Cell中TextBox的事件Bind到TextBoxDoubleClickHandler, TextBoxMouseDownHandler函式。
完整的Code如下,'建立空的DataTable Dim dtResult = New DataTable("SC_1Funs") dtResult.Columns.Add("FUN_ITEM_NAME", Type.GetType("System.String")) dtResult.Columns.Add("FUN_ITEM_ID", Type.GetType("System.String")) '新增資料到DataTable之中 dtResult.Rows.Add(New String() {"Fun1_name", "Fun1"}) dtResult.Rows.Add(New String() {"Fun2_name", "Fun2"}) dtResult.Rows.Add(New String() {"Fun3_name", "Fun3"}) dtResult.Rows.Add(New String() {"Fun4_name", "Fun4"}) dtResult.Rows.Add(New String() {"Fun5_name", "Fun5"}) DataGrid1.DataSource = dtResult '為Grid加入TableStyle&加入TextBox的Double Click AddTableStyleAndBindGridCellDBClick(DataGrid1) End Sub Private gridMouseDownTime As DateTime '記錄GridMouseDown下去的時間 ''' ----------------------------------------------------------------------------- ''' <summary> ''' 先加入TableStyle並加入Double Click Event ''' </summary> ''' ----------------------------------------------------------------------------- Private Sub AddTableStyleAndBindGridCellDBClick(ByRef rDataGrid As DataGrid) '設定TextGridText Handler Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle myGridTableStyle.MappingName = rDataGrid.DataSource.TableName rDataGrid.TableStyles.Add(myGridTableStyle) ' Note that DataGridColumnStyle objects will ' be created automatically for the first DataGridTableStyle ' when you add it to the GridTableStylesCollection.*/ For Each gcStyle As DataGridColumnStyle In myGridTableStyle.GridColumnStyles If TypeOf (gcStyle) Is DataGridTextBoxColumn Then '是文字,所以加入handler AddHandler CType(gcStyle, DataGridTextBoxColumn).TextBox.DoubleClick, _ New EventHandler(AddressOf TextBoxDoubleClickHandler) AddHandler CType(gcStyle, DataGridTextBoxColumn).TextBox.MouseDown, _ New MouseEventHandler(AddressOf TextBoxMouseDownHandler) End If Next End Sub Private Sub DataGrid1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DoubleClick MsgBox("DataGrid's DoubleClick Event") End Sub Private Sub TextBoxDoubleClickHandler(ByVal sender As Object, ByVal e As EventArgs) '轉呼叫Grid的DoubleClick處理函式 DataGrid1_DoubleClick(sender, e) End Sub Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGrid1.MouseDown gridMouseDownTime = DateTime.Now End Sub Private Sub TextBoxMouseDownHandler(ByVal sender As Object, ByVal e As MouseEventArgs) If (DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime)) Then '轉呼叫Grid的DoubleClick處理函式 DataGrid1_DoubleClick(sender, e) End If End Sub
參考資料
範例程式
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^