I have attached an UltraGrid to the right button of an UltraTextEditor, with the idea of creating a checked dropdown box. This works fine except I would like grid to automatically adjust its width so that all the text is viewable.
I seem to be able to do this by calling PerformingAutoResize on the visible columns, adding up their widths and making that sum the grid width.
The problem is when to do this. If I do it in InitializeLayout, the first time the grid is displayed, the width is incorrect, but after that it is correct. If I do it too soon, like right after I set the data binding, then the columns have not yet been created, so I can't do it.
Or if there is a better way, please let me know. Thanks.
If you are using v9.1 of the grid or later, there is built-in support for a multi-select checkbox list. So that would make things a lot easier.
If you are not using that version and cannot upgrade, then I would suggest doing the auto-sizing of the grid columns immediately after setting the data source, rather than waiting for InitializeLayout. What's probably happening is that InitializeLayoutis not firing until the first time the grid paints, which doesn't happen until after the first time you drop it down, by which time it is too late, because the dropdown size was already determined by the size of the grid before it was sized to the columns.
Unfortunately I will am using v7.3 of the grid and will not be able to upgrade. My problem seems to be that just setting the DataSource of the grid is not enough to create the columns so they can't be sized right after because they do not exist. Consider this example of a form with a button and an UltratextEditor:
private void button1_Click(object sender, EventArgs e) { //Create a Data Source DataTable dataTable = new DataTable(); dataTable.Columns.Add("column1", typeof(string)); dataTable.Columns.Add("column2", typeof(string));
dataTable.Rows.Add(new object[] { "row1Column1", "row1Column2" }); dataTable.Rows.Add(new object[] { "row2Column1", "row2Column2" });
//Create an UltraGrid and set the Data Source UltraGrid ultraGrid1 = new UltraGrid(); ultraGrid1.DataSource = dataTable;
//Number of columns == 0, they are not yet created so we can't size them int numColumns = ultraGrid1.DisplayLayout.Bands[0].Columns.Count;
//Add the grid to the text editor DropDownEditorButton controlButton = (DropDownEditorButton)ultraTextEditor1.ButtonsRight[0]; controlButton.Control = ultraGrid1; // Columns still do not exist numColumns = ultraGrid1.DisplayLayout.Bands[0].Columns.Count; }
Somewhere between setting the DataSource and when InitializeLayout is called, the columns are created. The question is where, and when I can set the Width property of the grid so that is sizes correctly when it drops down. Thanks for your help.
Bill
Hi Bill,
The grid probably has no columns because it has no BindingContext. Since you are not parenting the grid to the form, or any other control, it has no context.
Add the grid to the Controls collection of the form before you bind it. Or, try setting the grid's BindingContext to a new BindingContext.
Yes, that solves the issue. Thank You.
Can you post the code on how to do the auto-resize please? Thanks!