I've populated an unbound grid, and even put a delay in to make sure it's populated, but the auto resize after that does not work - i.e. if I click on the column boundary between columns 1 & 2, column 1 obediently shrinks down - but this code is not doing the same thing:
The code to populate and resize:
Private Sub Populate_Products_Grids() Try Grd_Prods.DataSource = Nothing Catch ex As Exception End Try Me.Cursor = Cursors.WaitCursor UseWaitCursor = True Application.DoEvents() Threading.Thread.Sleep(100) 'WAITING FOR THE CODE TO FINISH ConnectString = "Server = xx.xxxx.xx.xx; Log File=cprovider.log;Port=1972; Namespace=" & CompName & "; Password = TEST; User ID = TEST;" Try cnCache = New InterSystems.Data.CacheClient.CacheConnection(ConnectString) cnCache.Open() dsCache = New DataSet() daCache = New InterSystems.Data.CacheClient.CacheDataAdapter SQLstmt = "SELECT Convert(bit,0) as Choose, Product, Description FROM MVFILE.PRODUCTS" daCache.SelectCommand = New InterSystems.Data.CacheClient.CacheCommand(SQLstmt, cnCache, txCache) daCache.Fill(dsCache, "Sample_Person") dtCache = dsCache.Tables("Sample_Person") Grd_Prods.DataSource = dtCache Catch eLoad As Exception MessageBox.Show("An error has occurred: " + eLoad.Message) Application.Exit() End Try Threading.Thread.Sleep(100) 'WAITING FOR THE CODE TO FINISH Grd_Prods.DisplayLayout.Bands(0).Columns(1).PerformAutoResize(PerformAutoSizeType.AllRowsInBand) Grd_Prods.DisplayLayout.Bands(0).Columns(2).PerformAutoResize(PerformAutoSizeType.AllRowsInBand) UseWaitCursor = False Me.Cursor = Cursors.Default Application.DoEvents() Threading.Thread.Sleep(100) 'WAITING FOR THE CODE TO FINISH End Sub
In addition, this grid is read-only, which is what I want, sort of, but I'm not sure how this is accomplished globally (I used a preset read-only style), nor how to override it for a column. I have a checkbox column (created within the SQL statement, as you can see above), and I'm trying to let them check/uncheck boxes in that column while prohibiting any other kinds of editing, but it's not working:
My Initialize:
Private Sub Grd_Prods_InitializeLayout(sender As Object, e As InitializeLayoutEventArgs) Handles Grd_Prods.InitializeLayoute.Layout.Bands(0).Columns(0).CellActivation = Activation.AllowEdite.Layout.Bands(0).Columns(0).CellClickAction = CellClickAction.EditEnd Sub
Hello Samuel,
Yes, you are correct. Calling PerformAutoResize over a grid’s column should resize the column to its content. However, if you have set AutoFitStyle of the grid’s Layout to ResizeAllColumns and if the there is a free space after the columns all the columns will be enlarged proportionally. So please check if you set AutoFitStyle in your grid to ResizeAllColumns. More about column sizing you may find in our online documentation in this article “Resizing Columns”.
Regarding your second question what you need to do is set CellActivation to Disabled to all the columns you need to be read only and to AllowEdit to all other columns. You can use code like this:
For Each col As UltraGridColumn In e.Layout.Bands(0).Columns If col.Header.Caption <> "Your boolean column caption" Then col.CellActivation = Activation.Disabled Else col.CellActivation = Activation.AllowEdit End IfNext
You can also check this article in our knowledge base “HOWTO:How can I make a grid or a column, row, or cell in the UltraWinGrid disabled or read-only?”.
Please let me know if any additional questions on this matter arise.
I have tried all three autofit options. They have zero effect - just look at the middle column - sorting is also not working. Nor are several other things. Quite confused.
Code:
Private Sub Grd_Prods_InitializeLayout(sender As Object, e As InitializeLayoutEventArgs) Handles Grd_Prods.InitializeLayout e.Layout.Bands(0).Columns(1).CellActivation = Activation.AllowEdit e.Layout.Bands(0).Columns(2).CellActivation = Activation.AllowEdit e.Layout.AutoFitStyle = AutoFitStyle.ExtendLastColumn
End Sub
After filling grid:
Try cnCache = New InterSystems.Data.CacheClient.CacheConnection(ConnectString) cnCache.Open() dsCache = New DataSet() daCache = New InterSystems.Data.CacheClient.CacheDataAdapter SQLstmt = "SELECT Convert(bit,0) as Choose, Product, Description FROM MVFILE.PRODUCTS" daCache.SelectCommand = New InterSystems.Data.CacheClient.CacheCommand(SQLstmt, cnCache, txCache) daCache.Fill(dsCache, "Sample_Person") dtCache = dsCache.Tables("Sample_Person") Grd_Prods.DataSource = dtCacheCatch eLoad As Exception MessageBox.Show("An error has occurred: " + eLoad.Message) Application.Exit() End Try Grd_Prods.DisplayLayout.Bands(0).Columns(1).PerformAutoResize(PerformAutoSizeType.AllRowsInBand) Grd_Prods.DisplayLayout.Bands(0).Columns(2).PerformAutoResize(PerformAutoSizeType.AllRowsInBand) UseWaitCursor = False Me.Cursor = Cursors.Default Application.DoEvents()
NOTE: if I CLICK on the column separator between the middle and last column manually, it DOES resize down - so it's not that there's some big piece of data in the result set:
Ooops. No, I'm loading presets, not layouts!
Hi Samuel,
The key here is to make sure that the PerformAutoSize is called AFTER you load the layout or the preset. If you call it before, then loading the layout will essentially blow away any settings you applied in favor of the layout/preset.
OK. I understand that. But humor my abject stupidity: I selected the preset, but I didn't code a 'load' of it, though I am aware that it is indeed loaded. So, 'where and when' is it loaded, and how do I make sure that my code executes afterwards?
Actually, my code's executing afterwards, no? True, I executed my code first in initializelayout, which might be before, but now I am executing it after I populate the grid, yet it is still have no affect.
Thanks,.
Samuel Claiborne said:Actually, my code's executing afterwards, no?
Um... I'm not sure. How would I know when you are loading your preset? I don't see that anywhere in your code. Are you saying you loaded the preset at design-time? If so, then yes, I guess you are loading the preset before this code gets called. So that brings us back to square one, then. If that's the case, then I am out of ideas.
At this point, unless you can reproduce the problem in a small sample project and post it here, I'm not sure what else I can do to assist you.
Yes, loaded at design time. I've already uploaded a sample based on a case that was created from another forum entry. I've specified both the problem from that forum post and this one there (they seem like they may be related), so I think I'll leave it with that case for now.
Thanks.