Customizing UltraGrid Column Widths

I know this has been hit on in a few other posts… but… not being a “programmer”, does anybody have some simple instruction/code to add to the “Script Editor” to adjust the column widths on an UltraGrid?

I’ve seen some examples… but I don’t know WHERE this code needs to be added.

The first thing I do is register the grid’s “InitializeLayout” event in the form. This can be done with the form wizards. Basically this allows you to do something with that event.

It will look something like this for registration:

public void InitializeCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
		// Begin Wizard Added Variable Initialization

		// End Wizard Added Variable Initialization

		// Begin Wizard Added Custom Method Calls

		this.grdInput.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(this.grdInput_InitializeLayout);

///More stuff below
}

Deregistration:

public void DestroyCustomCode()
	{
		// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
		// Begin Wizard Added Object Disposal
		this.grdInput.InitializeLayout -= new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(this.grdInput_InitializeLayout);
}

And it will include a method to then write code inside of.

private void grdInput_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs args)
	{
		// ** Place Event Handling Code Here **
		grdInput.EpiAllowPaste = true;
		grdInput.EpiAllowPasteInsert = true;
		grdInput.InsertNewRowAfterLastRow = false;
	}

With that method in place and the event registered, you can then interact with it.
Here is a different example of some manipulation including adding columns and resizing stuff. Of note, the “Perform Auto Resize Columns” is particularly handy if you want it to self-size with the data.

private void grdSigners_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs args)
	{
		// ** Place Event Handling Code Here **
		
		//add mutually exclusive boolean to the grid to select primary signer 
		//args.Layout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.False;
	    args.Layout.Override.MinRowHeight = 20;
	    args.Layout.Override.DefaultRowHeight = 20;
	    
	    args.Layout.Bands[0].Columns.Add("Primary");
	    args.Layout.Bands[0].Columns["Primary"].DataType = typeof(bool);
	    args.Layout.Bands[0].Columns["Primary"].Header.VisiblePosition = 0;
	    //args.Layout.Bands[0].Columns["Primary"].EditorComponent = this.grdSigners;
	    
	    args.Layout.Bands[0].Columns["Primary"].Width = 16;
	    args.Layout.Bands[0].Columns["Primary"].MinWidth = 16;
	    args.Layout.Bands[0].Columns["Primary"].MaxWidth = 16;

		grdSigners.DisplayLayout.PerformAutoResizeColumns(false, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.VisibleRows);		
	}	

Does that help spur anything?

Also, the InitializeLayout event is just to get it set up. To have it interactively resize based on other events, it can be helpful to register different events, such as “AfterLayoutChange” to call the PerformAutoResizeColumns method as seen in these examples below:

private void grdCCs_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs args)
	{
		// ** Place Event Handling Code Here **
		grdCCs.DisplayLayout.PerformAutoResizeColumns(false, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.VisibleRows);
	}

	private void grdCCs_AfterCellUpdate(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs args)
	{
		// ** Place Event Handling Code Here **	
		grdCCs.DisplayLayout.PerformAutoResizeColumns(false, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.VisibleRows);
	}

	private void grdSigners_CellChange(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs args)
	{
		//Allows for only 1 row to be selected as "Primary"
		foreach(var item in grdSigners.Rows)
		{
			if(args.Cell.Row.Index != item.Index)
			{
				item.Cells["Primary"].Value = false;
				item.CancelUpdate();
			}
		}
	}
4 Likes

Thanks a lot, Aaron. This is a huge help! I hadn’t looked much at the Wizards as I’ve just been trying to piece together code from previous posts.

My company is in the midst of Epicor implementation, so its all rather new to us at the moment. The customization I’m currently working on is duplicating a dashboard we have in our legacy ERP system. Basically right now it is a Dashboard with several Ultragrids which are bound to different BAQ DataViews. That all worked well, again, once I found the right breadcrumbs to piece it together.

But, as you can see, the grid columns are not cooperating.

Following your examples, I was able to use the Event Wizard and add the InitializeLayout event (specifically for the “Keyword” grid as my test).

Within the added private void section, I then added:

args.Layout.Bands[0].Columns[“UDCodes_CodeDesc”].Width = 50;

< no resulting change >

I then tried:

Grid_Keywords.DisplayLayout.PerformAutoResizeColumns(false, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.VisibleRows);

< no resulting change >

I then noticed that you originally had a min and max width called out… so I wondered if I needed all (3). So I tried adding all three constraints:

args.Layout.Bands[0].Columns[“UDCodes_CodeDesc”].Width = 50;
args.Layout.Bands[0].Columns[“UDCodes_CodeDesc”].MinWidth = 50;
args.Layout.Bands[0].Columns[“UDCodes_CodeDesc”].MaxWidth = 50;

Again, no resulting change to the grid column widths. I still have the below.

image

I even tried all (4) of your width arguments together and it didn’t result in any change. I’m not getting any errors… just no change in column width.

Probably not the best example, as these all at least fit in the grid. But I have other keywords with descriptions that get cut off… so I’m trying to extend those.

Right now, the Dashboard loads, and the User can search the Part Number for which they want more information. Upon selecting the desired Part Number, the dashboard updates along with the multiple ultragrids. The problem is there is a TON of different UltraGrid Control Events. I’m wondering if I’m calling out the wrong “event” type?

Perhaps AfterCellUpdate… or AfterSelectChange… any thoughts?

The auto-resize will change the column widths to match the data in the grid, so if you’re looking for a fixed width, I wouldn’t call that method. One way to see if the event is firing is to pop a message box inside the method to see when it’s called. It will definitely require a little bit of investigating.

I am not really sure what I’m looking at in these screenshots, but perhaps it would help to explain the end goal a little. I see some nice clean looking grids, but I’m sure you’re looking for something specific :slight_smile:

My first image above is of the Dashboard w/ (5) separate Grids all bound to different BAQDVs.

The second image is of the “Keyword” grid… which shows a lot of open space.

image

So, in this case, I would like the “Description” column to extend over to the end of the grid container. Whether that’s by a static width length, or an auto-extend, either works for me.

I’m working on some trial & error efforts with various events as time allows.