Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hi,

i have derived a ColumnStyle of DataGrid to show ComboBox.

currently the ComboBox is visible only when i click a cell.

i.e. only while editing the cell.

i need to show it throughout the column even when i don't click.

simply, the cells of the column should appear as ComboBoxes.

 

if anybody has some idea please let me know.

Posted

code

 

here is the code

 

 


   Public Class CGridComboBoxStyle
       Inherits DataGridColumnStyle

       Public cgCombo As New ComboBox
       Private isEditing As Boolean
       Private m_ComboBoxStyle As ComboBoxStyle

       Public Sub New(ByVal MappingName As String)
           Me.cgCombo.Visible = False
           Me.MappingName = MappingName
           Me.NullText = String.Empty

           AddHandler Me.cgCombo.Leave, AddressOf HideControl

       End Sub

       Public Sub New(ByVal MappingName As String, _
                      ByVal Width As Integer, _
                      ByVal Alignment As HorizontalAlignment, _
                      ByVal HeaderText As String, _
                      ByVal NullText As String, _
                      ByVal Items() As String, _
                      ByVal ListDrop As ComboBoxStyle)
           Me.New(MappingName)
           Me.Width = Width
           Me.Alignment = Alignment
           Me.HeaderText = HeaderText
           Me.NullText = NullText
           If Items.Length > 0 Then
               Me.cgCombo.Items.AddRange(Items)
           End If
           m_ComboBoxStyle = ListDrop
           cgCombo.DropDownStyle = ListDrop
       End Sub

       Protected Overrides Sub Abort(ByVal rowNum As Integer)
           Try
               Me.cgCombo.Bounds = Rectangle.Empty
               isEditing = False
               Select Case m_ComboBoxStyle
                   Case ComboBoxStyle.DropDown
                       RemoveHandler cgCombo.TextChanged, AddressOf ComboBoxSelectedValueChanged
                   Case ComboBoxStyle.DropDownList
                       RemoveHandler cgCombo.SelectedValueChanged, AddressOf ComboBoxSelectedValueChanged
               End Select
               Invalidate()
           Catch ex As Exception
               'DisplayError(ex)
           End Try
       End Sub

       Protected Overrides Function Commit(ByVal dataSource As CurrencyManager, _
                                           ByVal rowNum As Integer) As Boolean

           Try
               Me.cgCombo.Bounds = Rectangle.Empty

               Select Case m_ComboBoxStyle
                   Case ComboBoxStyle.DropDown
                       RemoveHandler Me.cgCombo.TextChanged, AddressOf ComboBoxSelectedValueChanged
                   Case ComboBoxStyle.DropDownList
                       RemoveHandler Me.cgCombo.SelectedValueChanged, AddressOf ComboBoxSelectedValueChanged
               End Select

               If Not isEditing Then
                   Return True
               End If
               isEditing = False

               Dim value As String = Me.cgCombo.Text
               If value.CompareTo(NullText) = 0 Then
                   SetColumnValueAtRow(dataSource, rowNum, System.DBNull.Value)
               Else
                   SetColumnValueAtRow(dataSource, rowNum, value)
               End If

               Invalidate()

               ' data changed -> update array & show validation message
               'ValidateGridCellEntry(rowNum, Me.MappingName)

               Return True

           Catch ex As Exception
               'DisplayError(ex)
           End Try

       End Function

       Protected Overloads Overrides Sub Edit(ByVal [source] As CurrencyManager, _
                                              ByVal rowNum As Integer, _
                                              ByVal bounds As Rectangle, _
                                              ByVal [readOnly] As Boolean, _
                                              ByVal instantText As String, _
                                              ByVal cellIsVisible As Boolean)

           Try

               Select Case m_ComboBoxStyle
                   Case ComboBoxStyle.DropDown
                       RemoveHandler Me.cgCombo.TextChanged, AddressOf ComboBoxSelectedValueChanged
                   Case ComboBoxStyle.DropDownList
                       RemoveHandler Me.cgCombo.SelectedValueChanged, AddressOf ComboBoxSelectedValueChanged
               End Select

               Dim value As String
               If IsDBNull(GetColumnValueAtRow([source], rowNum)) Then
                   value = Me.NullText
               Else
                   value = CStr(GetColumnValueAtRow([source], rowNum))
               End If

               If cellIsVisible Then
                   Me.cgCombo.Bounds = bounds
                   Me.cgCombo.Text = value
                   Me.cgCombo.Visible = True
                   Select Case m_ComboBoxStyle
                       Case ComboBoxStyle.DropDown
                           AddHandler Me.cgCombo.TextChanged, AddressOf ComboBoxSelectedValueChanged
                       Case ComboBoxStyle.DropDownList
                           AddHandler Me.cgCombo.SelectedValueChanged, AddressOf ComboBoxSelectedValueChanged
                   End Select
               Else
                   Me.cgCombo.Text = value
                   Me.cgCombo.Visible = False
               End If

               If Me.cgCombo.Visible Then
                   DataGridTableStyle.DataGrid.Invalidate(bounds)
               End If

               'Focus the ComboBox so that user can scroll values
               Me.cgCombo.Focus()

           Catch ex As Exception
               'DisplayError(ex)
           End Try

       End Sub

       Protected Overrides Function GetPreferredSize(ByVal g As Graphics, _
                                                     ByVal value As Object) As Size
           Return New Size(100, Me.cgCombo.PreferredHeight)
       End Function

       Protected Overrides Function GetMinimumHeight() As Integer
           Return Me.cgCombo.PreferredHeight
       End Function

       Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, _
                                                       ByVal value As Object) As Integer
           Return Me.cgCombo.PreferredHeight
       End Function

       Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
                                               ByVal bounds As Rectangle, _
                                               ByVal [source] As CurrencyManager, _
                                               ByVal rowNum As Integer)
           Paint(g, bounds, [source], rowNum, False)
       End Sub

       Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
                                               ByVal bounds As Rectangle, _
                                               ByVal [source] As 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 Graphics, _
                                               ByVal bounds As Rectangle, _
                                               ByVal [source] As CurrencyManager, _
                                               ByVal rowNum As Integer, _
                                               ByVal backBrush As Brush, _
                                               ByVal foreBrush As Brush, _
                                               ByVal alignToRight As Boolean)


           Dim o As Object = Me.GetColumnValueAtRow([source], rowNum)
           Dim value As String

           If IsDBNull(o) Then
               value = Me.NullText
           Else
               value = CStr(o)
           End If

           Dim rect As Rectangle = bounds
           g.FillRectangle(backBrush, rect)
           g.DrawString(value, Me.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))

       End Sub

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

       Private Sub ComboBoxSelectedValueChanged(ByVal sender As Object, _
                                                ByVal e As EventArgs)
           If Not Me.isEditing Then
               Me.isEditing = True
               Select Case m_ComboBoxStyle
                   Case ComboBoxStyle.DropDown
                       RemoveHandler Me.cgCombo.TextChanged, AddressOf ComboBoxSelectedValueChanged
                   Case ComboBoxStyle.DropDownList
                       RemoveHandler Me.cgCombo.SelectedIndexChanged, AddressOf ComboBoxSelectedValueChanged
               End Select
               MyBase.ColumnStartedEditing(Me.cgCombo)
               Select Case m_ComboBoxStyle
                   Case ComboBoxStyle.DropDown
                       AddHandler Me.cgCombo.TextChanged, AddressOf ComboBoxSelectedValueChanged
                   Case ComboBoxStyle.DropDownList
                       AddHandler Me.cgCombo.SelectedIndexChanged, AddressOf ComboBoxSelectedValueChanged
               End Select
           End If
       End Sub

       Private Sub HideControl(ByVal sender As Object, _
                               ByVal e As EventArgs)

           Me.cgCombo.Bounds = Rectangle.Empty

       End Sub

       Protected Overrides Sub EnterNullValue()
           Me.cgCombo.Text = Me.NullText
       End Sub

   End Class

Posted

code

 

following two functions used in code are declared outside the class, so i have commented those lines.

 

DisplayError(ByVal ex As Exception)

 

ValidateGridCellEntry(ByVal rowNum As Integer, ByVal colName As String)

 

would really appreciate any help.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...