Notifications
Clear all

[Closed] dotNet add image to gridview

I need to make a gridview of image but it doesn’t work. Does anyone know how to make it?
Sample from C# here https://social.msdn.microsoft.com/Forums/windows/en-US/119222d6-0189-49ab-a01a-c5eeb201723e/datagridviewimagecolumn-dynamically-set-images-per-grid-row?forum=winforms


try (closeRolloutFloater myFloater) catch ()
global myFloater = newRolloutFloater "Chars"  400 500
global rTest = undefined
global filelist=getFiles ((getfilenamePath (getThisScriptFilename()))+"/data/*.jpg")

rollout rTest "rTest"  width:400 height:500
(
	dotNetControl gV "System.Windows.Forms.DataGridView" pos:[5,30] width:390 height:400
	on rTest open do
	(
		infoCol1 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
		infoCol1.headerText = "Info"
		infoCol2 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
		infoCol2.headerText = "Info"
		gV.columns.add infoCol1
		gV.columns.add infoCol2
		thumb = dotNetObject "System.Drawing.Bitmap" filelist[1]
		--thumb = (dotNetClass "System.Drawing.Image").FromFile filelist[1] true
		gV.rows.add()		
		gV.rows.add(thumb)		
		gV.rows[0].Cells[0].Value = thumb
		--gV.rows.add()		
		--gV.rows[0].Height = 50
		
	)
)
AddRollout  rTest  myFloater border:false


5 Replies
 MZ1

If you want to customize dotnet controls, I always prefer to use WPF version.

to get this working. pretty simple. To make it a decent script, a bit more work. First things first, This is it working…

try (destrpydialog rTest) catch ()

rollout rTest "rTest"  width:400 height:400
(
	local  filelist=getFiles ((getfilenamePath (getThisScriptFilename()))+"/data/*.jpg")

	local DataGridSizeMode= dotnetclass "system.windows.forms.DatagridviewAutoSizeColumnMode"

	dotNetControl gV "System.Windows.Forms.DataGridView" pos:[5,5] width:390 height:390
	on rTest open do
	(
		gv.AutoSizeRowsMode = gv.AutoSizeRowsMode.allCells
		
		infoCol1 = dotNetObject "System.Windows.Forms.DataGridViewImageColumn"
		infoCol1.headerText = "Thumb"
		infoCol1.width = 120
		infoCol2 = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
		infoCol2.headerText = "Info"
		infoCol2.autosizemode = DataGridSizeMode.fill
		gV.columns.add infoCol1 
		gV.columns.add infoCol2
	
		for i in filelist do 
		(
			thumb = (dotNetClass "System.Drawing.Image").FromFile i true
			gV.rows.add #(thumb, filenamefrompath i)				
		)	
	)
)
createdialog rTest 

Sorry I missed your reply
I just test it and found that DataGridSizeMode is undefined, code below
local DataGridSizeMode= dotnetclass “system.windows.forms.DatagridviewAutoSizeColumnMod e”

There’s a bug with the forum which sometimes puts extra spaces in, there’s one in that line at the end before the ‘e’ try removing it and see if it works then.

Thanks, it worked
By the way, I would like to do some more things:

  1. Row height: I tried but didn’t work gV.rows[1].Height = 120
  2. I would like to hide the header as well as column before the first column
  3. How to add image at center of cell. For example: cell heigh 120, width 120, add image 100×100 at center of cell
    Does anyone know how to do?