网站首页站长博客下载中心域名交易站长论坛域名主机免费电邮免费域名中文排行排名查询站长书库书籍教程下载
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
学院首页 网络编程 网页设计 图形图象 数 据 库 服 务 器 网络媒体 网络安全 个人专栏 站长CLUB 业界新闻 信息公告
 当前位置:首页 >> 网络编程 >> NET专区 >> 正文
公告通知
返回上级列表
资料搜索
相关文章
用户自定义控件的应用
c#.net常用函数和方法集
在VB中使用水晶报表的一种简易编
C#调用父类的父类的方法
浏览.NET Framework 2.0 类型库中
为.Text Blog 添加 计数器
编程实现邮件地址有效性检测 
VB/VB.NET/C#导出到Excel的方法
c#高性能在WEB端产生验证图片
用System.Web.Caching.Cache保存
向DataGrid控件中添加ComboBox控件
[ 来源:CSDN | 作者:未知 | 时间:2006-4-15 6:52:05 | 浏览:人次 ]
收藏到新浪ViVi 收藏到365KEY 收藏到我摘  字号选择〖    〗/ 双击滚屏 单击停止  
 

在前面看到了很多关于怎样向DataGrid中添加ComboBox控件的方法。使用的方法全部都是在VB6.0中的方法。

我还是要说说在CSND中发贴的朋友。

现在所谓的.NET编程人员,不知道是怎么了呢!只是停留在使用.NET的编程环境中。并没有真正的了解面向对象的.NET编程思想。

我现在就利用继承DataGridColumnStyle完成向DataGrid中添加ComboBox。

希望这样有助于大家了解真正的面向对象编程的思想。不要只是认为利用VB6.0中的某些方法就是.NET高手了。有这种思想的人都是菜鸟 (希望这么说没有得罪太多的朋友:)下面就是实现的代码:我使用的是VB.NET来完成的。

我熟悉C#,但是VB.NET只是大概了解一下。应该比一些人要高一点点吧!:)

见笑了!由于时间关系没有协注释,请见谅!

Public Class DataGridComboColumn
    Inherits DataGridColumnStyle

    Public WithEvents DGCombo As ComboBox = New ComboBox
    Private isEditing As Boolean
    Private _strSelectedText As String

    Public Sub New()
        MyBase.New()
        DGCombo.Visible = False
    End Sub

    Protected Overrides Sub Abort(ByVal rowNum As Integer)
        isEditing = False
        RemoveHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
        Invalidate()

    End Sub

    Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
        DGCombo.Bounds = Rectangle.Empty
        AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
        If isEditing = False Then
            Return True
        End If

        isEditing = False
        Try
            DGCombo.Text = DGCombo.Text
        Catch ex As Exception
            DGCombo.Text = String.Empty
        End Try

        Try
            Dim value As String = _strSelectedText
            SetColumnValueAtRow(dataSource, rowNum, value)
        Catch ex As Exception
            Abort(rowNum)
            Return False
        End Try
        Invalidate()
        Return True

    End Function

    Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
        Dim value As String
        Try
            value = CType(GetColumnValueAtRow(source, rowNum), String)
        Catch ex As Exception
            SetColumnValueAtRow(source, rowNum, DGCombo.Text)
        End Try

        value = CType(GetColumnValueAtRow(source, rowNum), String)

        If (cellIsVisible) Then
            DGCombo.Bounds = New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)
            DGCombo.Text = value
            DGCombo.Visible = True
            AddHandler DGCombo.SelectedValueChanged, AddressOf DGCombo_SelectedValueChanged
        Else
            DGCombo.Text = value
            DGCombo.Visible = False
        End If

        If DGCombo.Visible = False Then
            DataGridTableStyle.DataGrid.Invalidate(bounds)
        End If
    End Sub

    Protected Overrides Function GetMinimumHeight() As Integer

    End Function

    Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer

    End Function

    Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

    End Function

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)
        Paint(g, bounds, source, rowNum, True)
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
        Paint(g, bounds, source, rowNum, Brushes.Red, Brushes.Blue, alignToRight)
    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, _
                              ByVal bounds As System.Drawing.Rectangle, _
                              ByVal source As System.Windows.Forms.CurrencyManager, _
                              ByVal rowNum As Integer, _
                              ByVal backBrush As System.Drawing.Brush, _
                              ByVal foreBrush As System.Drawing.Brush, _
                              ByVal alignToRight As Boolean)

        Dim strDate As String
        Dim rect As RectangleF
        Try
            strDate = DGCombo.Text
            strDate = CType(GetColumnValueAtRow(source, rowNum), String)
        Catch ex As Exception
            SetColumnValueAtRow(source, rowNum, DGCombo.Text)
            strDate = CType(GetColumnValueAtRow(source, rowNum), String)
        End Try

        rect.X = bounds.X
        rect.Y = bounds.Y
        rect.Height = bounds.Height
        rect.Width = bounds.Width

        g.FillRectangle(backBrush, rect)
        rect.Offset(0, 2)
        rect.Height -= 2

        g.DrawString(strDate, Me.DataGridTableStyle.DataGrid.Font, foreBrush, rect)

    End Sub

    Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
        MyBase.SetDataGridInColumn(value)
        If Not (DGCombo.Parent Is Nothing) Then
            DGCombo.Parent.Controls.Remove(DGCombo)
        End If
        If Not (value Is Nothing) Then
            value.Controls.Add(DGCombo)
        End If
    End Sub

    Private Sub DGCombo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGCombo.SelectedValueChanged
        isEditing = True
        MyBase.ColumnStartedEditing(DGCombo)
        _strSelectedText = DGCombo.Text
        If _strSelectedText Is Nothing Then
            _strSelectedText = String.Empty
        End If
    End Sub
End Class

以下是使用方法!

    Private Sub frmDataGrid_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call TEST()
    End Sub

    Private Sub TEST()
        Dim DT As New DataTable("TEST")
        DT.Columns.Add(New DataColumn("ID"))
        DT.Columns.Add(New DataColumn("COMBO"))
        Call AddGridStyle()
        Call ADDDATA(DT)
        DBGrid.DataSource = DT

    End Sub

    Private Sub AddGridStyle()

        Dim DBGridStyle As DataGridTableStyle
        Dim IDColumn As DataGridTextBoxColumn
        Dim DCColumn As DataGridComboColumn

        DBGridStyle = New DataGridTableStyle
        DBGridStyle.MappingName = "TEST"

        IDColumn = New DataGridTextBoxColumn
        IDColumn.MappingName = "ID"
        IDColumn.HeaderText = "ID"
        IDColumn.Alignment = HorizontalAlignment.Center
        IDColumn.Width = 50
        DBGridStyle.GridColumnStyles.Add(IDColumn)

       DCColumn = New DataGridComboColumn
        Dim i As Integer
        For i = 0 To 25
            DCColumn.DGCombo.Items.Add((i + 1).ToString("00000"))
        Next
        DCColumn.DGCombo.DropDownWidth = 120
        DCColumn.MappingName = "COMBO"
        DCColumn.HeaderText = "COMBO"
        DCColumn.Alignment = HorizontalAlignment.Center
        DCColumn.Width = 60
        DBGridStyle.GridColumnStyles.Add(DCColumn)

       DBGrid.TableStyles.Add(DBGridStyle)

    End Sub

    Private Sub ADDDATA(ByRef DT As DataTable)
        Dim DROW As DataRow
        Dim intRow As Integer

        For intRow = 0 To 9
            DROW = DT.NewRow()
            DROW.Item("ID") = Format(intRow + 1, "000")
            DROW.Item("COMBO") = Format(intRow + 1, "00000")
            DT.Rows.Add(DROW)
        Next

        DT.AcceptChanges()

    End Sub

那么随时在DataGrid中所指定为Combo列中单击。Combo就出现了



[发送给好友]  [打印本页]  [关闭窗口]  [返回顶部]   转载请注明来源:http://edu.chinaz.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
栏目编辑: 设计风 责任编辑: keke
原始作者: 未知 录入时间: 2006-4-15 6:52:05
信息来源: CSDN 投稿信箱: Edu#chinaz.com
设为首页 - 加入收藏 - 关于我们 - 广告服务 - 版权申明 - 友情链接 - 联系方式 - 总编信箱 - 会员投稿