Getting Datagrid Columns Keypress,Keyup, Keydown and other e

80酷酷网    80kuku.com

  datagrid'It might be easier to read the code if you
'copied this to a text program

'After your form has a functioning datagrid and dataset,
'do the following steps.
'1) Name and declare each column as a textbox as follows

Friend WithEvents Column1 As TextBox
Friend WithEvents Column2 As TextBox

'2) Create the tablestyles.
'Here is the code microsoft gives to do just that.
'(Again I did not write this part; its copied and 'pasted from MS!!)

Private Sub AddTables(ByVal myDataGrid As DataGrid, _
                    ByVal myDataSet As DataSet)
      Dim t As DataTable
      For Each t In myDataSet.Tables
                    Dim myGridTableStyle As DataGridTableStyle = New _
                    DataGridTableStyle()
                    myGridTableStyle.MappingName = t.TableName
                    myDataGrid.TableStyles.Add(myGridTableStyle)
                    ' Note that DataGridColumnStyle objects will
                    ' be created automatically for the first DataGridTableStyle
                    ' when you add it to the GridTableStylesCollection.*/
     Next
End Sub

'3) Add the next line of code in the Form Load Event
      'Use your own variable names for the datagrid and dataset)
      AddTables(DataGrid1, DataSet1)
      'Creates the variable to identify object
      Dim TempColumn As DataGridTextBoxColumn
      'Sets the variable to the datagrid column
      TempColumn = DataGrid1.TableStyles(0).GridColumnStyles(0)
      'Set the column (textboxe actually) equal to the
      'temporary column you just created.
      Column1 = TempColumn.TextBox
      'Repeat for each column you made. Notice the next line is
      'the same as the previous except it now identifies the
      'next column (one instead of zero) and the line after
      'that is the next column you created in step 1.
      TempColumn = DataGrid1.TableStyles(0).GridColumnStyles(1)
      Column2 = TempColumn.TextBox

'4) Write the code for whatever event you want to fire. Notice
'after you declared the variables withevents then they now appear
'as a class in the drop down menu. Here is example to see the
'keypress events fire for the two columns you identified.


      Private Sub Column1_Keypress(ByVal sender As Object, _
                    ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                    Handles Column1.KeyPress

                    MsgBox("You have pressed the " & e.KeyChar)

      End Sub

      Private Sub Column2_Keypress(ByVal sender As Object, _
                    ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                    Handles Column2.KeyPress

                    MsgBox("You have pressed the " & e.KeyChar)

      End Sub 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: