新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:Getting Datagrid Columns' Keypress,Keyup, Keydown and other events to Fire(一个很好的

Getting Datagrid Columns' Keypress,Keyup, Keydown and other events to Fire(一个很好的

新客网 XKER.COM 2003-07-12 来源: 收藏本文
'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 
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐