none
Ich brauche hilfe bei'm dataGridView RRS feed

  • Frage

  • ich habe nun schon einige Tage damit zugebracht mich in das Thema DataGridview und die benutzung von Bildern einzulesen.  Dabei bin ich auf viele Lösungen gekommen, jedoch nicht auf das was ich brauche.

    Hintergrund ist der: Ich habe ein Datagridview in dem sollen einzelne zeilen einer Zelle unterschiedlich farbig dargestellt werden. das das nur mit einer DataGridviewImageCell funktioniert unter der verwendung einer RTF Schnittstelle ist mir nun klar. Jedoch erzeugt der Quelltext einen Fehler den ich nicht deuten / beheben kann.

    Zur verdeutlichung habe ich hier den Quelltext meiner Klassen:

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Drawing
    Imports System.Drawing.Printing
    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms
    
    
    ''' <summary>
    ''' http://support.microsoft.com/default.aspx?scid=kb;en-us;812425
    ''' The RichTextBox control does not provide any method to print the content of the RichTextBox. 
    ''' You can extend the RichTextBox class to use EM_FORMATRANGE message 
    ''' to send the content of a RichTextBox control to an output device such as printer.
    ''' </summary>
    Public Class RichTextBoxPrinter
    
        'Convert the unit used by the .NET framework (1/100 inch) 
        'and the unit used by Win32 API calls (twips 1/1440 inch)
        Private Const anInch As Double = 14.4
    
        Private Const WM_USER As Integer = 1024
    
        Private Const EM_FORMATRANGE As Integer = (WM_USER + 57)
    
        Private Declare Function SendMessage Lib "user32" _
      Alias "SendMessageA" ( _
      ByVal hwnd As Long, _
      ByVal wMsg As Long, _
      ByVal wParam As Long, _
      ByVal lParam As IntPtr) As Long
    
    
        ' Render the contents of the RichTextBox for printing
        '    Return the last character printed + 1 (printing start from this point for next page)
        Public Overloads Shared Function Print(ByVal richTextBoxHandle As IntPtr, ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer
            'Calculate the area to render and print
            Dim rectToPrint As RECT
            rectToPrint.Top = CType((e.MarginBounds.Top * anInch), Integer)
            rectToPrint.Bottom = CType((e.MarginBounds.Bottom * anInch), Integer)
            rectToPrint.Left = CType((e.MarginBounds.Left * anInch), Integer)
            rectToPrint.Right = CType((e.MarginBounds.Right * anInch), Integer)
            'Calculate the size of the page
            Dim rectPage As RECT
            rectPage.Top = CType((e.PageBounds.Top * anInch), Integer)
            rectPage.Bottom = CType((e.PageBounds.Bottom * anInch), Integer)
            rectPage.Left = CType((e.PageBounds.Left * anInch), Integer)
            rectPage.Right = CType((e.PageBounds.Right * anInch), Integer)
            Dim hdc As IntPtr = e.Graphics.GetHdc
            Dim fmtRange As FORMATRANGE
            fmtRange.chrg.cpMax = charTo
            'Indicate character from to character to 
            fmtRange.chrg.cpMin = charFrom
            fmtRange.hdc = hdc
            'Use the same DC for measuring and rendering
            fmtRange.hdcTarget = hdc
            'Point at printer hDC
            fmtRange.rc = rectToPrint
            'Indicate the area on page to print
            fmtRange.rcPage = rectPage
            'Indicate size of page
            Dim res As Double
            Dim wparam As IntPtr = IntPtr.Zero
            wparam = New IntPtr(1)
            'Get the pointer to the FORMATRANGE structure in memory
            Dim lparam As IntPtr = IntPtr.Zero
            lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
            Marshal.StructureToPtr(fmtRange, lparam, False)
            'Send the rendered data for printing 
            res = SendMessage(richTextBoxHandle, EM_FORMATRANGE, wparam, lparam)
            'Free the block of memory allocated
            Marshal.FreeCoTaskMem(lparam)
            'Release the device context handle obtained by a previous call
            e.Graphics.ReleaseHdc(hdc)
            ' Release and cached info
            SendMessage(richTextBoxHandle, EM_FORMATRANGE, CType(0, IntPtr), CType(0, IntPtr))
            'Return last + 1 character printer
            Return True
        End Function
    
        Public Overloads Shared Function Print(ByVal ctl As RichTextBox, ByVal width As Integer, ByVal height As Integer) As Image
            Dim img As Image = New Bitmap(width, height)
            Dim scale As Single
            Dim g As Graphics = Graphics.FromImage(img)
            ' --- Begin code addition D_Kondrad
            ' HorizontalResolution is measured in pix/inch         
            scale = (CType((width * 100), Single) / img.HorizontalResolution)
            width = CType(scale, Integer)
            ' VerticalResolution is measured in pix/inch
            scale = (CType((height * 100), Single) / img.VerticalResolution)
            height = CType(scale, Integer)
            ' --- End code addition D_Kondrad
            Dim marginBounds As Rectangle = New Rectangle(0, 0, width, height)
            Dim pageBounds As Rectangle = New Rectangle(0, 0, width, height)
            Dim args As PrintPageEventArgs = New PrintPageEventArgs(g, marginBounds, pageBounds, Nothing)
            Print(ctl.Handle, 0, ctl.Text.Length, args)
            Return img
        End Function
    
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure RECT
    
            Public Left As Integer
    
            Public Top As Integer
    
            Public Right As Integer
    
            Public Bottom As Integer
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure CHARRANGE
    
            Public cpMin As Integer
    
            'First character of range (0 for start of doc)
            Public cpMax As Integer
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure FORMATRANGE
    
            Public hdc As IntPtr
    
            'Actual DC to draw on
            Public hdcTarget As IntPtr
    
            'Target DC for determining text formatting
            Public rc As RECT
    
            'Region of the DC to draw to (in twips)
            Public rcPage As RECT
    
            'Region of the whole DC (page size) (in twips)
            Public chrg As CHARRANGE
        End Structure
    End Class


    Imports System
    Imports System.Windows.Forms
    
    Public Class CalendarColumn
        Inherits DataGridViewColumn
    
        Public Sub New()
            MyBase.New(New RTFCell)
        End Sub
    
        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
    
                ' Ensure that the cell used for the template is a CalendarCell.
                If (value IsNot Nothing) AndAlso _
                    Not value.GetType().IsAssignableFrom(GetType(RTFCell)) _
                    Then
                    Throw New InvalidCastException("Must be a CalendarCell")
                End If
                MyBase.CellTemplate = value
    
            End Set
        End Property
    
    End Class
    
    Public Class RTFCell
        Inherits DataGridViewImageCell
    
        Public Sub New()
            ' Use the short date format.
            'Me.Style.Format = "d"
        End Sub
    
    
    
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)
    
            ' Set the value of the editing control to the current cell value.
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                dataGridViewCellStyle)
    
            Dim ctl As DataGridViewExtended = _
                CType(DataGridView.EditingControl, DataGridViewExtended)
            'ctl.Text = CType(Me.v, String)
    
        End Sub
    
    
    
        Public Overrides ReadOnly Property EditType() As Type
            Get
                ' Return the type of the editing contol that CalendarCell uses.
                Return GetType(DataGridViewExtended)
            End Get
        End Property
    
        Public Overrides Property ValueType() As Type
    
            Get
                ' Return the type of the value that CalendarCell contains.
                Return GetType()
            End Get
    
            Set(ByVal value As Type)
    
            End Set
        End Property
    
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                ' Use the current date and time as the default value.
                'Return "Hallo"
                Return RichTextBoxPrinter.Print(New RichTextBox, 100, 100)
            End Get
        End Property
    
    
    
    End Class
    
    Class DataGridViewExtended
        Inherits RichTextBox
        Implements IDataGridViewEditingControl
    
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
    
        Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Me.SelectionColor = Color.AliceBlue
            MyBase.OnKeyPress(e)
        End Sub
    
        Public Sub New()
            'Me.Format = DateTimePickerFormat.Short
        End Sub
    
        Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue
    
            Get
    
                Return RichTextBoxPrinter.Print(Me, 100, 100)
            End Get
    
            Set(ByVal value As Object)
    
            End Set
    
        End Property
    
        Public Function GetEditingControlFormattedValue(ByVal context _
            As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
    
            Return RichTextBoxPrinter.Print(Me, 100, 100)
    
        End Function
    
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As  _
            DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
    
            Me.Font = dataGridViewCellStyle.Font
            Me.ForeColor = dataGridViewCellStyle.ForeColor
            Me.BackColor = dataGridViewCellStyle.BackColor
    
        End Sub
    
        Public Property EditingControlRowIndex() As Integer _
            Implements IDataGridViewEditingControl.EditingControlRowIndex
    
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
    
        End Property
    
        Public Function EditingControlWantsInputKey(ByVal key As Keys, _
            ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
            Implements IDataGridViewEditingControl.EditingControlWantsInputKey
    
            ' Let the DateTimePicker handle the keys listed.
            Select Case key And Keys.KeyCode
                Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
                    Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp, _
                    Keys.F5, Keys.F6, Keys.F7
                Case Keys.F2
                    Me.SelectionBackColor = Color.Red
                    Return True
    
                Case Else
                    Return Not dataGridViewWantsInputKey
            End Select
    
        End Function
    
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
            Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
    
            ' No preparation needs to be done.
    
        End Sub
    
        Public ReadOnly Property RepositionEditingControlOnValueChange() _
            As Boolean Implements _
            IDataGridViewEditingControl.RepositionEditingControlOnValueChange
    
            Get
                Return False
            End Get
    
        End Property
    
        Public Property EditingControlDataGridView() As DataGridView _
            Implements IDataGridViewEditingControl.EditingControlDataGridView
    
            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As DataGridView)
                dataGridViewControl = value
            End Set
    
        End Property
    
        Public Property EditingControlValueChanged() As Boolean _
            Implements IDataGridViewEditingControl.EditingControlValueChanged
    
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set
    
        End Property
    
        Public ReadOnly Property EditingControlCursor() As Cursor _
            Implements IDataGridViewEditingControl.EditingPanelCursor
    
            Get
                Return MyBase.Cursor
            End Get
    
        End Property
    
        Protected Sub OnValueChanged(ByVal eventargs As EventArgs)
    
            ' Notify the DataGridView that the contents of the cell have changed.
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(eventargs)
    
        End Sub
    
        Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
            MyBase.OnLeave(e)
            MyBase.Rtf = Me.Rtf
        End Sub
    
    End Class
    
    Public Class asdf
        Inherits Form
    
        Private dataGridView1 As New DataGridView()
    
        <STAThreadAttribute()> _
        Public Shared Sub Main()
            Application.Run(New Form1())
        End Sub
    
        Public Sub New()
            Me.dataGridView1.Dock = DockStyle.Fill
            Me.Controls.Add(Me.dataGridView1)
            Me.Text = "DataGridView calendar column demo"
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
            Handles Me.Load
    
            Dim col As New CalendarColumn()
            Me.dataGridView1.Columns.Add(col)
            Me.dataGridView1.RowCount = 5
            Dim row As DataGridViewRow
            'Dim i As Image
            For Each row In Me.dataGridView1.Rows
                'row.Cells(0).Value = DateTime.Now
            Next row
    
        End Sub
    
    End Class


    Falls jemand eine Lösung hat wäre ich sehr Dankbar...

    Okeanos
    Dienstag, 4. August 2009 11:49

Antworten

  • Hallo Okeanos,

    Dein Code ist so nicht kompilierbar.
    Auf Codeprojekt gibt es eine Lösung in C# RichTextBox Cell in a DataGridView
    die an stark an Deinen Code erinnert.

    Vor allem fehlt aber etwas, woran man erkennen könnte, was Du am Ende erreichen willst
    (das asdf am Ende ist doch eher sinnfrei).
     
    Wenn Du das zusammenstellen könntest, könnte man man eher etwas erfinden,
    und sei es obiges Projekt für ein VB.NET Vorlage zu portieren.

    Gruß Elmar

    Mittwoch, 5. August 2009 08:59

Alle Antworten

  • Hallo Okeanos,

    Dein Code ist so nicht kompilierbar.
    Auf Codeprojekt gibt es eine Lösung in C# RichTextBox Cell in a DataGridView
    die an stark an Deinen Code erinnert.

    Vor allem fehlt aber etwas, woran man erkennen könnte, was Du am Ende erreichen willst
    (das asdf am Ende ist doch eher sinnfrei).
     
    Wenn Du das zusammenstellen könntest, könnte man man eher etwas erfinden,
    und sei es obiges Projekt für ein VB.NET Vorlage zu portieren.

    Gruß Elmar

    Mittwoch, 5. August 2009 08:59
  • Hallo Okeanos_85,

    Hat Dir die Antwort geholfen?

    Grüße,
    Robert

    Dienstag, 8. September 2009 07:02
  • Hallo Okeanos_85,

    Ich gehe davon aus, dass die Antwort Dir weitergeholfen hat.
    Solltest Du noch "Rückfragen" dazu haben, so gib uns bitte Bescheid.

    Grüße,
    Robert

    Sonntag, 13. September 2009 18:35