Notifications
Clear all

[Closed] dotnet datagrid not getting selection count

Why does this not display the selected indices count after selection is made?
It may seem like a lot of code but it’s just UI setup. The actual command is simple and short.
It always shows zero?



rollout groupToolRO "groupTool"
(
	local groups = #() --Array of groupData structs
	local uiGroupList
	local uiNodeList
	struct groupData (name = "", nodes = #())

	dotNetControl uiContainer "ContainerControl" pos:[0,0]
	
	fn AddGroup name:"" nodes:#() = 
	(
		nodes = makeUniqueArray (for i = 1 to (random 5 15) collect ("teapot00" + i as string))
		d = groupData()
		d.name = name
		d.nodes = makeUniqueArray nodes

		append groups d
	)

	fn ResizeUI =
	(
		local width = groupToolRO.width
		local height = groupToolRO.height

		uiContainer.width = width
		uiContainer.height = height
	)

	fn RefreshGroupsUI ctrl:uiGroupList = 
	(
		local groupItems = #()

		for g in groups do
		(
			newRow = dotNetObject "DataGridViewRow"
			newRow.height = 22

			ctrl.rows.add newRow

			newRow.cells.item[0].value = g.name
			newRow.cells.item[0].ToolTipText = "Node Count: " + g.nodes.count as string
		)
		uiGroupList.update()

		--PopulateNodesUI()
	)

	fn GenerateDataGrid =
	(
		local ctrl = dotNetObject "DataGridView"
		ctrl.MultiSelect = true
		ctrl.AllowUserToAddRows = false
		ctrl.AllowUserToDeleteRows = false
		ctrl.AllowUserToResizeRows = false
		ctrl.AllowUserToResizeColumns = true
		ctrl.AllowUserToOrderColumns = false
		ctrl.AutoSize = True
		ctrl.ShowEditingIcon = false
		ctrl.RowHeadersVisible = false
		ctrl.ColumnHeadersBorderStyle = ctrl.ColumnHeadersBorderStyle.None
		ctrl.ColumnHeadersHeightSizeMode = ctrl.ColumnHeadersHeightSizeMode.DisableResizing
		ctrl.SelectionMode = ctrl.SelectionMode.CellSelect
		ctrl.ColumnHeadersVisible = true
		ctrl.BorderStyle = ctrl.BorderStyle.FixedSingle
		ctrl.AutoResizeColumns()
		ctrl
	)

	fn GenerateColumns columns:#() =
	(
		local dnColumns = #()
		for n in columns do
		(
			local newColumn = dotNetObject "DataGridViewTextBoxColumn"
			newColumn.Resizable = newColumn.Resizable.true
			newColumn.AutoSizeMode = newColumn.AutoSizeMode.Fill
			newColumn.DefaultCellStyle.Alignment.MiddleLeft
			newColumn.HeaderText = n
			newColumn.ReadOnly = true
			newColumn.sortMode = newColumn.sortMode.NotSortable
			append dnColumns newColumn
		)
		dnColumns
	)

	fn GenerateRows ctrl:undefined data:#() =
	(
		for d in data do
		(
			newRow = dotNetObject "DataGridViewRow"
			newRow.height = 22
			
			--newRow.tag = dotNetMXSValue d.node
			ctrl.rows.add newRow
			newRow.cells.item[0].value = d
			newRow.cells.item[0].ToolTipText = "Node Count: " + 10 as string
		)
	)


	fn clickedGroups s e = 
	(
		enableaccelerators = off
		s.Focus()
		print s.SelectedRows.count

	)

	fn StyleContainer ctrl:undefined =
	(
		uiSplitCtrl = dotNetObject "splitContainer"
		uiSplitCtrl.dock = uiSplitCtrl.dock.fill
		uiSplitCtrl.AutoScroll = true
		uiSplitCtrl.SplitterWidth = 6
		uiSplitCtrl.SplitterDistance = 60
		ctrl.controls.add uiSplitCtrl
		
		-- Control
		uiGroupList = GenerateDataGrid()
		uiGroupList.dock = uiGroupList.dock.fill
		uiGroupList.Columns.AddRange (GenerateColumns columns:#("Groups"))
		uiSplitCtrl.panel1.controls.add uiGroupList
		-- control handlers
		dotNet.addEventHandler uiGroupList "Click" clickedGroups
	)

	on groupToolRO open do
	(
		-- ui control styling
		StyleContainer ctrl:uiContainer
		ResizeUI()

		--test data
		AddGroup name:"great"
		AddGroup name:"monkey"
		AddGroup name:"donuts"

		RefreshGroupsUI()
	)

	on groupToolRO resized val do
	(
		ResizeUI()
	)
)
createDialog groupToolRO width:400 height:300 style:#(#style_SysMenu, #style_titlebar, #style_minimizebox, #style_resizing)
 

4 Replies
 lo1

You’re registering to the DGV’s ‘Click’ event. When that is called, the selection has not yet been updated. Register to the ‘SelectionChanged’ event instead.

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionchanged(v=vs.110).aspx

For your help I’ll try that out when I get home.

dotNet.addEventHandler uiGroupList “SelectionChanged” doubleClickGroups

didn’t fix the bug still…

fixed!

Also, keep in mind that The SelectionMode property must be set to FullRowSelect or RowHeaderSelect for the SelectedRows property to be populated with selected rows.