none
Add Image to DataGridView Column with text

    Question

  • Is there a way to add an image to a column that also contains text? preferably one from an imagelist control..
    Monday, March 01, 2010 8:32 PM

Answers

  • Thank you Calle for your friendly help.

     

    Hi ghendric,

     

     

    In your scenario, you can consider creating RichTextBox Columns or cells into DataGridView, to display text and image.

     

    Because:

    1.    You can create your own DataGridView column types (e.g. RichTextBoxColumn) with cells that host controls.

    Please check this document for detailed instruction and code sample:
    http://msdn2.microsoft.com/en-us/library/7tas5c80.aspx
    How to: Host Controls in Windows Forms DataGridView Cells

    The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing. To do this, you must define classes that derive from DataGridViewColumn and DataGridViewCell. You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface.

    The document shows code example demonstrating how to create a calendar column. The cells of this column display dates in ordinary text box cells, but when the user edits a cell, a DateTimePicker control appears. In order to avoid having to implement text box display functionality again, the CalendarCell class derives from the DataGridViewTextBoxCell class rather than inheriting the DataGridViewCell class directly.

    Reference: How to add DateTimepicker control within a DataGridView column type
    http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/3ab0b15f-82dc-42ed-afc6-b5c47621e630/

     

    2.    RichTextBox can be used to display/host text and image as the following code sample.

    Public Class Form1  

     

        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  

            'Set text style/format in RichTextBox  

            RichTextBox1.SelectionAlignment = HorizontalAlignment.Center  

            RichTextBox1.SelectionColor = Color.Blue  

            RichTextBox1.SelectionFont = New Font("Courier", 10, FontStyle.Bold)  

     

            'Insert Text into RichTextBox  

            RichTextBox1.AppendText("Image Description" & vbCrLf)  

     

            'Insert image into RichTextBox  

            Dim img As Image = Image.FromFile("D:\VBProject\WindowsVista.jpg")  

            Clipboard.SetDataObject(img)  

            Dim df As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)  

            If Me.RichTextBox1.CanPaste(df) Then 

                Me.RichTextBox1.Paste(df)  

            End If 

     

        End Sub 

     

    End Class 

    Trackback: http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/dc535258-eeed-4953-bef9-a3d42db22d88/

     

     

    ·         You can use RichTextBox control to display formatted text and

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 02, 2010 3:30 AM
    Moderator

All replies

  • Do you want to add an image to the column header, or to the cells of one column?
    Either way, you have override the defualt paint behaviour to get both image and text inside one cell or header. 

    /Calle


    - Still confused, but on a higher level -
    Monday, March 01, 2010 9:06 PM
  • Thank you Calle for your friendly help.

     

    Hi ghendric,

     

     

    In your scenario, you can consider creating RichTextBox Columns or cells into DataGridView, to display text and image.

     

    Because:

    1.    You can create your own DataGridView column types (e.g. RichTextBoxColumn) with cells that host controls.

    Please check this document for detailed instruction and code sample:
    http://msdn2.microsoft.com/en-us/library/7tas5c80.aspx
    How to: Host Controls in Windows Forms DataGridView Cells

    The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing. To do this, you must define classes that derive from DataGridViewColumn and DataGridViewCell. You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface.

    The document shows code example demonstrating how to create a calendar column. The cells of this column display dates in ordinary text box cells, but when the user edits a cell, a DateTimePicker control appears. In order to avoid having to implement text box display functionality again, the CalendarCell class derives from the DataGridViewTextBoxCell class rather than inheriting the DataGridViewCell class directly.

    Reference: How to add DateTimepicker control within a DataGridView column type
    http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/3ab0b15f-82dc-42ed-afc6-b5c47621e630/

     

    2.    RichTextBox can be used to display/host text and image as the following code sample.

    Public Class Form1  

     

        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  

            'Set text style/format in RichTextBox  

            RichTextBox1.SelectionAlignment = HorizontalAlignment.Center  

            RichTextBox1.SelectionColor = Color.Blue  

            RichTextBox1.SelectionFont = New Font("Courier", 10, FontStyle.Bold)  

     

            'Insert Text into RichTextBox  

            RichTextBox1.AppendText("Image Description" & vbCrLf)  

     

            'Insert image into RichTextBox  

            Dim img As Image = Image.FromFile("D:\VBProject\WindowsVista.jpg")  

            Clipboard.SetDataObject(img)  

            Dim df As DataFormats.Format = DataFormats.GetFormat(DataFormats.Bitmap)  

            If Me.RichTextBox1.CanPaste(df) Then 

                Me.RichTextBox1.Paste(df)  

            End If 

     

        End Sub 

     

    End Class 

    Trackback: http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/dc535258-eeed-4953-bef9-a3d42db22d88/

     

     

    ·         You can use RichTextBox control to display formatted text and

    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, March 02, 2010 3:30 AM
    Moderator
  • Hi ghendric,

    Does this help? If you have any further questions or concerns, please feel free to let us know.


    Best regards,

    Martin Xie

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, March 04, 2010 4:57 AM
    Moderator