problem with fitting datagridview height according to the content - vb.net

I have a datagridview whose styling is kept as follows
dgvCreatinine.AutoGenerateColumns = False
dgvCreatinine.AutoSize = False
dgvCreatinine.AutoResizeRows()
dgvCreatinine.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
dgvCreatinine.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)
dgvCreatinine.DataSource = dtTemp
It is appearing as shown in the attached image
As you can see the gridview is not resizing to the content along row wise. Could someone tell me how this can be accomplished? Thanks in advance.

You can just set the height of the entire control using this formula
DataGridViewHeight = HeaderHeight + NumberOfRows * RowHeight
Code:
dgvCreatinine.Height =
dgvCreatinine.ColumnHeadersHeight _
+ dgvCreatinine.Rows.OfType(Of DataGridViewRow).First().Height * dgvCreatinine.Rows.Count()
This works, but could use some tweaking. For example, there may be a better way to get the row height. And you may also need to add a pixel or two depending on borders. Also, if you have an underlying DataSource, you could get the rows count from it.
That code should be called whenever a row is added or removed, or DataBindings are updated etc.

Related

Do not show partially visible rows in datagridview

I try to implement the following in winform vb net project (I see this work in an app written in delphi).
I wish to hide or set visibility to false of the bottom row that partially visible in dgv that is docked to fill.
I tried to implement something like this:
DataGridView1.Rows(DataGridView1.DisplayedRowCount(true) - 1).Visible = False
I think it should be called during DataBindingComplete and Resize/scroll events, but it doesn't work.
Do you have any ideas / solutions?
So what I use on one of my datagridviews is:
Dim ind As Integer = 0
ind = DataGridView1.Rows.Count - 1
DataGridView1.Rows(ind).Visible = False
which hides the last displayed row of the datagridview.
You requirement sounds somewhat odd. Your comment ”I wish to hide or set visibility to false of the bottom row that partially visible in dgv that is docked to fill.”... I am curious how you would know this is the last row? Is it not possible that there are more rows below the last one visible? If the scroll bars are available you should see the vertical one if rows go beyond its bounding box. If one of the rows is cut in half by the bounding box and there is more than 1 row below this row, then making invisible/hiding/deleting that row will simply move the next one up.
Since the DataGridView is docked you may have to resize the rows manually if you do not want the rows to be split by the bounding box. Another possible solution is to use the DataGridViews AutoSizeRowsMode Like below.
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
This will set the rows so that a row will not be chopped if it is outside the bottom of the bounding box. The rows will auto size to fit evenly There are seven (7) AutoSizeRowsMode options and I am guessing one of them may do what you are looking for. I am guessing DisplayedCells may work for what you describe. If the grid is re-sized often, you may have to implement this row resizing. Hope this helps.

Completely resize DataGridView in VB.NET

I am having issues resizing a DataGridView in a VB.NET application. The DataGridView is not bound to a data source, as all data is entered into it manually.
It is currently docked in a TableLayoutPanel, set to Fill, and I would expect it to automatically resize to fit its assigned cell, but it seems to have a minimum size, at which I can not shrink it any further. This is a problem because the tablet PC I am deploying to has a much smaller resolution, so the Windows must scale properly. The TableLayoutPanels are keeping everything in the correct position, but it is crucial that my grids scale as well so that the end user can see the bottom scroll bar and all the records in the table.
This problem is pretty much the same as the one mentioned here.
If you go to AlternatingRowsDefaultCellStyle in the properties window of the datagridviewer there is Padding feature that defaults to 0,0,0,0 which represents each direction. Perhaps if you reduce the padding it will autosize up instead of being limited on the upper end moving down.
Use AutoSizeMode and FillWeight properties of columns in your datagridview
'Column 1
Dim columnindex As Int32 = Me.DataGridView1.Columns.Add("One", "One")
With Me.DataGridView1.Columns(columnindex)
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.FillWeight = 80 'This can be used as percent value for column width
End With
'Column 2
columnindex = Me.DataGridView1.Columns.Add("Two", "Two")
With Me.DataGridView1.Columns(columnindex)
.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.FillWeight = 20
End With
Me.DataGridView1.Rows.Add({"sample text", "1"})
Me.DataGridView1.Rows.Add({"another sample text", "2"})

Listview AutoReziseColumns not working

ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
I'm having an issue since both of these lines separately produce the same result, which is resizing both the ColumnContent and the HeaderSize simultaneously.
I've searched and come up with no answer for this, i only need to resize by the HeaderSize but with the above example it seems not to be working correctly... or am i missing something?
And i've tested with more than one listview control...
Edit: I'm using it like this:
Private Sub UserListResize()
If Me.UserList.InvokeRequired Then
Me.UserList.Invoke(Sub() UserListResize())
Else
UserList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End If
End Sub
I'm calling this when i'm finished with adding items on the listview.
They dont do the exactly the same thing, but it is sometimes hard to tell them apart.
HeaderSize resizes each column to the greater of HeaderText.Length or longest cell content, thereby never clipping the header text.
ColumnContent resizes to longest cell content which will clip header text if the HeaderText is longer then the content. Test this with a column containing 1 or 2 digits/characters, but a long header like "FooBar Counter of Foo".
Neither mode will clip column text though, which means when you are economic with header text, it can look like they do the same thing. Other things to consider:
The LV must have items in it for ColumnContent to be meaningful
Inspite of Auto in the name, it is not a persistent setting - as soon as you add something else the layout may be incorrect, so you may have to reset it periodically.
You can always lay them out how you want and set AllowColumnResize to False
You set AutoResizeColumn on a per column basis (note it is singular)
Individual column widths can be "auto sized" using the Width Property and the Magic Numbers of -1 (set to content) or -2 (size to Header). This allows you to only apply the AutoResize logic to only some columns or use a different setting depending on the column:
For n As Integer = 0 To myLV.Columns.Count -1 Step 2 ' just do every other
myLV.Columns.Width = -2 ' -2 = size to column header text
' -1 = size to longest content
Next n
One place this is very handy is when adding columns dynamically. Rather than guessing at the TextExtent, you can set the new column's width to something at least somewhat applicable to start with, then change it the first time content is added. Reference: MSDN ColumnHeader Width Property.

Fix radgrid height with paging

How to fix the size of radgrid irrespective of records.
I set the paging with pagesize = 5.
When radgrid has 5 rows its height will be ok.
If the grid has 2 rows its fits to the 2 rows height.
How to fix the radgrid height.
I think you got my problem.
I'm doing like this in page_load
radgrid.Height=Unit.Pixel(200);
Hi can anyone answer my question..?
In the Databound() event handler, check for the number of records.
int c = RadGrid1.Items.Count()
Then try your:
RadGrid1.Height = Unit.Pixel( Whatever you want ) * c;
set the scroll height instead of setting the height. This makes sure that the whole data area height is maintained.
RadGrid1.ClientSettings.Scrolling.ScrollHeight = Unit.Pixel(200);
This article may help as well.
http://www.telerik.com/help/aspnet-ajax/grid-height-vs-scrollheight.html

The control or subform control is too large for this location on resize

I have a simple form in Access 2003. The form has a List control and a button. I wanted to make the list to resize with the form (only vertically) while the button remains at the bottom right of the List. I'm using the following code on the form's resize event.
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
This works fine as I resize the form, until the form height gets to a certain height. Then I get this error;
Run-time error '2100':
The control or subform control is too large for this location
This error is occurring at the line where I'm assigning the commandButton.Top. If I remove this line then the list height changes fine. I don't have any subforms in the form.
Does anyone know why this is happening?
Thanks
I think it is because you need to resize the detail section of the form first.
Try changing the code to
Me.Section(0).Height = Me.InsideHeight
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
Came by here (as many have) with the same problem and then realised my issue. Be mindful when resizing a control to a larger size with regard to the order of setting properties.
I would recommend setting the TOP and LEFT positions before HEIGHT and WIDTH.
Although my final sized control should have fitted OK once resized, I had originally tried setting the WIDTH first which attempted to enlarge the control exceeding the width of the form. My application threw the 2100 error at that point. I really hope this helps someone! Also, don't forget to set dimensions in TWIPS which is set as INCHESS x 1440 (or CM x 566.9291), ie: .Width = 10 * 566.9291 to set a control width to 10cm.
I get this same error if I set the width to greater the 31680.
With a little more research, I notice MS Access only allows a form width to be 22" wide. 22" = 31680 TWIPS.
So my workaround solutions it to add a check:
If newWidth > 31680 Then newWidth = 31680