Posts Tagged ‘Technology’

I was trying hard to find a suitable way to show text and image in same cell in a DataGridView Column. Thanks to DotNetMonitor I found the code to extend the column so that it can do that. However, as I am pretty new to C#, I did not know how to use this code. Once I figured it out how simple it is, first thing that came to my mind was to share this. This information might be helpful for someone like me who is totally new to C#. All you need to do is:

  1. Copy the code from provided link and create a new file.
  2. This way, You’ll get two new classes, TextAndImageColumn and TextAndImageCell.
  3. Change the type of required column to the TextAndImageColumn
  4. Populate the text values just as you do normally.
  5. To show the image, All you need to do is, set the image property when you are populating the data. You may need to type cast the column object to TextAndImageCell for this. Below is the sample line of code:
    ((TextAndImageCell)(myDataGridView[columnIndex, rowPos])).Image = Image.FromFile(“myImage.ico”);
  6. just replace the myImage.ico with the path to your image.

I hope this helps someone.

    Tags: , , ,