Notifications
Clear all

[Closed] .NET Datatable/Datagrid question

I’m having a bit of an issue with getting a Datagrid working in a rollout:

 (
	rollout ro_test "test"
	(
		fn createDt =
		(
			-- Add Some Columns to the Datagrid
			local dt = dotnetobject "system.data.DataTable""Test"
			local column1 = dotnetobject "System.Data.DataColumn" "id" (dotnetclass "System.Int32")
			local column2 = dotnetobject "System.Data.DataColumn" "Name" (dotnetclass "System.String")
			dt.columns.add Column1
			dt.columns.add Column2

			for i = 0 to 2 do 
			(
				local row=#(dotnetobject "System.Int32" i, dotnetobject "System.String" (i as string)) 
				dt.rows.add row
			)
			dt
		)
		
		dotnetcontrol dg "System.Windows.Forms.DataGrid"
		
		on ro_test open do
		(
			dg.Location = dotNetObject "System.Drawing.Point" 10 10
			dg.Width = 165
			dg.Height = 120
			dg.Visible = true
			dg.DataSource = createDt()
		)
	)
	
	createdialog ro_test 500 500
 )

I’m no .NET master and I’m sure the answer is pretty simple. When I create the rollout, I get a blank Datagrid. In fact, it is the same whether I comment out the ‘dg.Datasource = createDt()’ line. What gives?

EDIT: I can begin to get around this using a DataGridView and avoiding the Datatable, but I’m curious if anyone knows why this doesn’t work.

4 Replies
1 Reply
(@zeboxx2)
Joined: 11 months ago

Posts: 0

Haven’t the foggiest… your code should work, and does work in a .NET Form… just not in the rollout.
DataGrid -has- been superceded by DataGridView, so it may just be using bad display methods; either way, I’ve had zero luck trying to get it to display anything but the caption title in a rollout.

Ah, I didn’t know DataGrid was deprecated/superceded. Anyway, I’ve got the code working correctly with a DataGridView, so things are all peachy.
Here’s the code, in case it comes up in anyone’s search and can be helpful:


(
	dotnet.loadassembly "System.Data" 
	rollout ro_test "test"
	(		
		dotnetcontrol dg "System.Windows.Forms.DataGridView"
		
		on ro_test open do
		(
			local colWidth = 160
			local fromCol = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
			fromCol.headerText = "Original Textures"
			fromCol.readOnly = true
			fromCol.frozen = true
			fromCol.width = colWidth
			
			local toCol = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
			toCol.headerText = "New Textures"
			toCol.width = colWidth
			
			dg.columns.add fromCol
			dg.columns.add toCol
			
			dg.Location = dotNetObject "System.Drawing.Point" 10 10
			dg.Width = 400
			dg.Height = 400
			dg.Visible = true
			
			for i = 1 to 5 do
			(
				local row = #(dotNetObject "System.String" ("row str"))
				dg.rows.add row
			)
		)
	)
	
	createdialog ro_test 500 500
)

Well, it’s not entirely deprecated… from MSDN:

Although the DataGridView control replaces and adds functionality to the DataGrid control of previous versions, the DataGrid control is retained for both backward compatibility and future use if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.

And from that last URL:
The only feature that is available in the DataGrid control that is not available in the DataGridView control is the hierarchical display of information from two related tables in a single control. You must use two DataGridView controls to display information from two tables that are in a master/detail relationship.

However, that type of phrasing (from the first source) looks familiar and typically means “use at your own peril; it’ll be entirely neglected from hereon and will eventually be removed after grave errors occur from its continued use”

Still doesn’t explain why-oh-why it doesn’t work in the rollout, however. All the usual suspects of data not getting displayed (not binding, etc.) offered no solace.

Hi Rob, I took a look at this too and came up with the same outcome. In my copy of Visual Studio, they list Datagridview as a toolbox item, but no Datagrid (i guess you’d have to add it manually), so they are obviously pushing things that way. Since you said you had it working with DGV i didnt post the results but it was the same as yours!

BTW the lines –

dg.Location = dotNetObject "System.Drawing.Point" 10 10
			dg.Width = 400
			dg.Height = 400

could also be included here –

dotnetcontrol dg "System.Windows.Forms.DataGridView" pos:[10,10] width:400 height:400

but it does makes sense to have all the initialization stuff in one place in the function.

I’d ideally like it if you could pass other properties to dotnetcontrols like backcolor etc, rather than cluttering up the open event. All other classes in max like box etc take multiple parameters on creation!