How to Change Form Width Depending on Datagridview's Data - vb.net

I have a form that contains a datagridview. The cells in the datagridview expand or contract depending on text width.
How can I make the parent form in which the data grid is docked also contract or expand to the cell (total of all cells) width?
Hence I want the parent form to change in the row (width) size and not in the column (height) size?
Im using vb.net

I would recommend just making it so that users can resize the program to the width that they want, and have the datagridview adjust it's width to fit the forms width. If you adjust it based on the columns, then it could cause issues where the form is bigger than the users screen. The easiest way to do this is by using the Dock and Anchor attributes.
If you really want to have the form adjust it's width to fit the columns, you would have to loop through the columns and add the width values together after it is databound. After you get the total width, you would have to set your forms width
The code would look something like this:
Dim intWidth As Integer
For Each dgvcc As DataGridViewColumn In DataGridView1.Columns
intWidth += dgvcc.Width
Next
Me.Width = intWidth

Related

How to change the width of an item in a listview?

I have a winforms application with a listview that shows 6 items. The problem is that each item's width is too long so the user has to scroll to the right to see all of each item.
How can I shorten the width of each rectangle for each item to match the width of the data inside the item/rectangle???
Thank you
Use this code:
ListviewName.View = View.Details
ListviewName.Columns.Clear()
ListviewName.Columns.Add("Columnn1", CInt(ListviewName.Width / divide))
add all your column.
divide value is depends on how many column in your Listview, but that's if you want all of your column size is equal.

DataGridView adjusting Height with scroll bars

I have a datagrid view that has a full height of .height=500. If client click on a button, I need to change the height of the data grid view because I need to display a groupbox with a few textboxes and comboboxes. I am able to do that with...
datagridview1.height=150
What my problem is - if I first populate the datagrid with lets say 20 rows of data, and then I edit the height, I am not able to scroll down the grid to see clients that were cut off by my height adjustment. So I tried something like this....
datagridview1.ScrollBars = Windows.Forms.ScrollBars.Horizontal
But it's the same thing. No scorll bars for this datagridview.

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"})

Data Grid View VB.net 2013 , How many rows and which are visible in window?

I have a datagridview control with 10 rows on my form window.
Now lets suppose the size of the datagridview in the form can only display 5 rows to user and user has to use vertical scroll bar to see the other 5 rows.
now my question is :
Is there any way to know which 5 rows are currently visible to the user according to the position of scroll bar.
Actually in my program currently i am updating the all the rows value one by one of that datagridview table continuously.
Now i want to only update the rows that are visible in the window. so how can i know what rows are visible according to scroll bar position.
Thanks in advance
The DataGridView control exposes a VerticalScrollingOffset property which can be used to determine how far the view is scrolled. If you know the height of each row in the DataGridView in pixels, you can then calculate which rows of the table are currently visible and refresh them.
The height of each row can be determined through the DataGridViewRow property Height. This defaults to the font height, plus 9 pixels - if you're using the same font size in all rows this should be consistent.
EDIT: Further digging through the documentation shows that each DataGridViewRow exposes a Displayed property that returns true when the row is visible on the user's screen. Checking that would be much easier!
MSDN on DataGridViewRow.Displayed property

Items should be arrange into three columns in CheckedListBox or listview or listbox

I have used CheckedListBox control in vb.net application.
I have several items as follow, item 1, item 2, item 3........item 100.
Now, i want all these items to arrange into three columns and then appear verticle scrollbar.
But, in CheckedListBox, it appears as horizontally. i want it to appear horizontally for just three columns and then vertical scroll should enable.
Can you please suggest if possible in CheckedListBox control or any other control and if possible then how i would have to set its property to achieve this.
I don't think you can do that in a CheckedListBox....
You could create a FlowLayoutPanel, with a fixed width and add AutoScroll = True.
Then add a number of CheckBox controls to it.
You could have problems regarding column alignment and margins. If that's the case, you can set the checkbox Autosize = False and make them a little larger (in order to contain the text)
Or you can reduce the Height of the checkboxes to reduce spaces (is this what you want?)
eg. Height = 15
Use repeat direction =horizontal and repeat column =3 for checkbox list