VB . Net : Hebérger un contrôle label comme DataGridColumnStyle ?

cs_Asterion Messages postés 2 Date d'inscription jeudi 6 mars 2003 Statut Membre Dernière intervention 26 mars 2004 - 6 mars 2003 à 18:49
strikel Messages postés 5 Date d'inscription jeudi 2 janvier 2003 Statut Membre Dernière intervention 2 décembre 2003 - 21 oct. 2003 à 12:28
:question)
Dans un Datagrid, comment fait-on pour utiliser autre chose que Texbox (DataGridTextBox), comme par exemple des images ou des labels.

Je remercie toute personne qui prendra le temps de me répondre.

Amicalement,
A voir également:

2 réponses

Pikashoute Messages postés 91 Date d'inscription jeudi 27 février 2003 Statut Membre Dernière intervention 23 décembre 2005
7 mars 2003 à 11:34
Je suis désolée mais il me semble qu'il n'y a que deux choix possibles :

Textbox ou Case à cocher
0
strikel Messages postés 5 Date d'inscription jeudi 2 janvier 2003 Statut Membre Dernière intervention 2 décembre 2003
21 oct. 2003 à 12:28
Ca devrait t'aider,

Public Class DataGridLabelColumn
    Inherits DataGridColumnStyle

    Private _xMargin As Integer = 2
    Private _yMargin As Integer = 1
    Private _Label As Label
    'Private _OldVal As String = String.Empty
    'Private _InEdit As Boolean = False

    Public Property Label() As Label
        Get
            Return MyClass._Label
        End Get
        Set(ByVal Value As Label)
            MyClass._Label = Value
        End Set
    End Property

    Public Sub New()
        MyClass._Label = New Label()
        MyClass._Label.Visible = False
    End Sub

#Region "Heritage de DataGridColumnStyle"
    Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
        RollBack()
        HideLabel()
        EndEdit()
    End Sub
    Protected Overloads Overrides Function Commit(ByVal DataSource As CurrencyManager, ByVal RowNum As Integer) As Boolean
        HideLabel()
        Return True
        'Plus utile
        Try
            Dim Value As Object = MyClass._Label.Text
            If NullText.Equals(Value) Then
                Value = Convert.DBNull
            End If
            SetColumnValueAtRow(DataSource, RowNum, Value)
        Catch e As Exception
            RollBack()
            Return False
        End Try
        EndEdit()
        Return True
    End Function
    Protected Overloads Overrides Sub ConcedeFocus()
        MyClass._Label.Visible = False
    End Sub
    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)

        If Not InstantText Is Nothing Then
            MyClass._Label.Text = InstantText
        Else
            MyClass._Label.Text = Me.DataGridTableStyle.DataGrid.Item(Me.DataGridTableStyle.DataGrid.CurrentCell.RowNumber, Me.DataGridTableStyle.DataGrid.CurrentCell.ColumnNumber).ToString
        End If

        Dim OriginalBounds As Rectangle = Bounds

        If CellIsVisible Then
            Bounds.Offset(MyClass._xMargin, MyClass._yMargin)
            Bounds.Width -= MyClass._xMargin * 2
            Bounds.Height -= MyClass._yMargin
            MyClass._Label.Bounds = Bounds
            MyClass._Label.Visible = True
        Else
            MyClass._Label.Bounds = OriginalBounds
            MyClass._Label.Visible = False
        End If

        MyClass._Label.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
        MyClass._Label.Focus()

        If MyClass._Label.Visible Then
            DataGridTableStyle.DataGrid.Invalidate(OriginalBounds)
        End If

    End Sub
    Protected Overloads Overrides Function GetMinimumHeight() As Integer
        Return MyClass._Label.PreferredHeight + MyClass._yMargin
    End Function
    Protected Overloads Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal Value As Object) As Integer
        Dim NewLineIndex As Integer = 0
        Dim NewLines As Integer = 0
        Dim ValueString As String = Me.GetText(Value)
        Do
            While NewLineIndex <> -1
                NewLineIndex = ValueString.IndexOf("r\n", NewLineIndex + 1)
                NewLines += 1
            End While
        Loop

        Return FontHeight * NewLines + MyClass._yMargin
    End Function
    Protected Overloads Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal Value As Object) As Size
        Dim Extents As Size = Size.Ceiling(g.MeasureString(GetText(Value), Me.DataGridTableStyle.DataGrid.Font))
        Extents.Width += MyClass._xMargin * 2 + DataGridTableGridLineWidth
        Extents.Height += MyClass._yMargin
        Return Extents
    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)
        Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))
        PaintText(g, Bounds, Text, AlignToRight)
    End Sub
    Protected Overloads 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 Text As String = GetText(GetColumnValueAtRow(Source, RowNum))
        PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
    End Sub
    Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Value As DataGrid)
        MyBase.SetDataGridInColumn(Value)
        If Not (MyClass._Label.Parent Is Value) Then
            If Not (MyClass._Label.Parent Is Nothing) Then
                MyClass._Label.Parent.Controls.Remove(MyClass._Label)
            End If
        End If

        If Not (Value Is Nothing) Then Value.Controls.Add(MyClass._Label)
    End Sub
    Protected Overloads Overrides Sub UpdateUI(ByVal Source As CurrencyManager, ByVal RowNum As Integer, ByVal InstantText As String)
        MyClass._Label.Text = GetText(GetColumnValueAtRow(Source, RowNum))
        If Not (InstantText Is Nothing) Then
            MyClass._Label.Text = InstantText
        End If
    End Sub
#End Region

#Region "Méthodes privées"
    Private ReadOnly Property DataGridTableGridLineWidth() As Integer
        Get
            If Me.DataGridTableStyle.GridLineStyle = DataGridLineStyle.Solid Then
                Return 1
            Else
                Return 0
            End If
        End Get
    End Property
    Private Sub EndEdit()
        Invalidate()
    End Sub

    Private Function GetText(ByVal Value As Object) As String
        If Value Is System.DBNull.Value Then Return NullText

        If Not Value Is Nothing Then
            Return Value.ToString
        Else
            Return String.Empty
        End If

    End Function

    Private Sub HideLabel()
        If MyClass._Label.Focused Then
            Me.DataGridTableStyle.DataGrid.Focus()
        End If
        MyClass._Label.Visible = False
    End Sub

    Private Sub RollBack()

    End Sub

    Private Sub PaintText(ByVal g As Graphics, ByVal Bounds As Rectangle, ByVal Text As String, ByVal AlignToRight As Boolean)

        Dim BackBrush As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)
        Dim ForeBrush As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)
        PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
    End Sub

    Private Sub PaintText(ByVal g As Graphics, ByVal TextBounds As Rectangle, ByVal Text As String, ByVal BackBrush As Brush, ByVal ForeBrush As Brush, ByVal AlignToRight As Boolean)

        Dim Rect As Rectangle = TextBounds
        Dim RectF As RectangleF = RectF.op_Implicit(Rect) ' Convert to RectangleF
        Dim Format As StringFormat = New StringFormat()

        If AlignToRight Then
            Format.FormatFlags = StringFormatFlags.DirectionRightToLeft
        End If

        Select Case Me.Alignment
            Case Is = HorizontalAlignment.Left
                Format.Alignment = StringAlignment.Near
            Case Is = HorizontalAlignment.Right
                Format.Alignment = StringAlignment.Far
            Case Is = HorizontalAlignment.Center
                Format.Alignment = StringAlignment.Center
        End Select

        Format.FormatFlags = Format.FormatFlags Or StringFormatFlags.NoWrap
        g.FillRectangle(Brush:=BackBrush, Rect:=Rect)

        Rect.Offset(0, MyClass._yMargin)
        Rect.Height -= MyClass._yMargin
        g.DrawString(Text, Me.DataGridTableStyle.DataGrid.Font, ForeBrush, RectF, Format)
        Format.Dispose()

    End Sub
#End Region
End Class
0
Rejoignez-nous