Scrolling with multiple controls in panel C# - c#

I have a Form with 4 Controls. One panel, which contains 2 DataGridViews and 1 Label. First there is a datagridview, then the label and then the last datagridview. The Form has a specific size like 600x400. I also want that the first datagridview should have the exact height of all cell height + header cell height from the first datagridview. If its bigger than 400, there should be a scrollbar on the right. If the user scrolls down, he should the the label and the 2. DataGridView. If the Height is less than 400, maybe 300, then it should show already the label and a scrollbar on the right. How could I do that?
Thanks!

The Panel control has a property called "AutoScroll" that you can set to true (in the properties pane while you are in design mode.) This will handle showing and hiding the scrollbar based on the size of the child controls. Make sure that the panel has a fixed height, rather than using AutoSize.
If I am understanding correctly, you want the first DataGridView to size itself based on the size of its contents. To do that, just set AutoSize = true on the DataGridView and it will resize based on its contents.
If you want more specific help, you can post your code and what you have tried, and you may get a better answer.

Related

How can I link a component's Size to the Window size?

I am making a simple WinForms program.
I would like to link a component's size to the size of the Window.
Let's say the user enlarges or shrinks the Window by dragging it's borders: I would like that a component gets bigger when the Window does and vice versa.
Let's pretend that we have two Buttons, in the center of the Window, side by side: I want to make them the same size filling the entire width of the Window.
How can I do that?
Sample procedure, using a TableLayoutPanel and 2 Buttons:
Add a TableLayoutPanel to the Form
Edit the Columns and Rows collections so that you have 2 Columns, both sized at 50% and one Row, set to Autosize.
Set the Location.X of the TableLayoutPanel to 0 and adjust its width to the width of the Form.
Set the TLP Anchor property to Left and Right
Adjust-drag the Row height to be ~twice the size of the Button it will host
Add one Button to the Form
Adjust the appearance of the Button as required.
CTRL-Drag the Button to create an exact duplicate
Add the two Buttons to the two cells of the TableLayoutPanel
SHIFT-Select both Buttons and set the Dock property to DockStyle.Fill
You can now adjust the Margin property of the Buttons (still both selected, so the same settings will apply to both) to modify the space between the controls
Re-adjust the TableLayoutPanel's only Row height as needed.
Extra: if you have only these controls on the Form, you may want to fix the MinimumSize of the Form, to avoid that, when the Form is resized, your controls are shrinked beyond recognition, ruining the overall layout: resize the Form in the Designer to a point where the hosted controls layout is compromised, find a suitable minimum size and use this measure as the Form's MinimumSize property. The MinimumSize can be set using only the Width or the Height measure (e.g., (100, 0)). This limits the Form's Width but not the Height. Or the opposite, of course.
If, when dragging the Buttons inside the TableLayoutPanel, the Buttons
are not automatically inserted at the Top-Left position of the cell
and instead they appear positioned in a random place, then the
TableLayoutPanel has gone rogue and needs to be put down. Delete it
and drop another on the Form. Rinse and repeat.
This may happen if you tamper with the layout a bit. Better start over than trying to correct the problem.
TableLayoutPanel Control Overview

How to handle an empty datagridview?

I am using the datagridview from the toolbox and I want it to look good.
Everything works perfectly but I need some styling advice here.
Because I have a default sized form and when I have many view rows in my datagridview it looks bad because half of the view is the forms background and not my data grid view. But I want it to seem like the whole thing is the table.
DataGridView rows, columns, and headers can change size as a result of many different occurrences:
User resize - Users can make size adjustments by dragging or double-clicking row, column, or header dividers.
Control resize - In column fill mode, column widths change when the control width changes; for example, when the control is docked to its parent form and the user resizes the form.
Cell value change - In content-based automatic sizing modes, sizes change to fit new display values.
Method call - Programmatic content-based resizing lets you make opportunistic size adjustments based on cell values at the time of the method call.
Property setting - You can also set specific height and width values.
By default, user resizing is enabled, automatic sizing is disabled, and cell values that are wider than their columns are clipped.
For more information look here
I am assuming you want the datagrid view to adjust automatically to the size of the form. In this case, Set the anchor property of the control to hook to all sides of the parent - top, bottom, left, and right.
If you want your form looks like a table you can use Dock property for datagridview. and dock this tool to the form
and then you must do some changes on datagridview view like this:
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.RowHeadersVisible = false;
adding some rows and columns for see the setting result
dataGridView1.Columns.Add("column1", "column1");
dataGridView1.Columns.Add("column2", "column2");
dataGridView1.Columns.Add("column3", "column3");
dataGridView1.Columns.Add("column4", "column4");
dataGridView1.Rows.Add(10);
and then you can set form back color and datagridview back color as same color.

How to reserve space in Win Form TableLayoutPanel control

I have a question about WinForm TableLayoutPanel control. For example, if I hide the control in the column. it will change the size.
Is there a way to not paint the control in a tablelayoutpanel with column autosize so that the column will still have the control size?
Using Control.Visible = false will make the column width 0.
I need something like hidden in WPF Grid.
You can place a Panel in the TLP and place the control on the panel. Set the panel's background color to the same as the TLP, and it will be invisible. Then hide the control but leave the panel.
This should work fine if your control is a fixed size. If the control size varies, it might be a bit more tricky to do this. You will need to vary the size of the Panel too. Setting it to AutoSize = true and AutoSizeMode = GrowOnly might work.

change the height of statusStrip

I have a simple windows form with a statusStrip in VS2010, and no matter what I tried, the height of statusStrip does not change, what is proper way of changing the height??
thanks
I just changed the StatusStrip size without problems...
Create a new Form.
Create a StatusStrip.
Set its property "Dock" to "None".
Set its property "Autosize" to "False".
Set its property "Size" with Height = 'the height you need' (I have put it to 100).
If you want, now you can Dock to "Bottom" again.
Add a ProgressBar: it will be 100 Height.
Let me know if this works for you
None of the mentioned approaches worked. Eventually this is the working solution for us:
First, use a TableLayoutPanel with a row at 100% and bottom row with absolute height of 24 (or any desired height you want).
Second, drag and drop the ToolStrip control into the bottom row, and setting the Dock property to Fill. This forces the ToolStrip control to fill the bottom cell of the TableLayoutPanel. Now you have a toolstrip with height 24.

How do I make a DataGridView that doesn't snap its rows to the top of the control?

We've used the DataGridView extensively in our client. Our customers want to be able to enter large amounts of text into a row - too much to be displayed in the height of the DataGridView - and scroll through them using the main scrollbar - i.e. the DataGridView's scrollbar.
However, the DataGridView will only scroll such that a row is snapped to the top of the control; if a row is larger than the DataGridView then you can only ever see the top of it.
How do I make a DataGridView that doesn't snap its rows to the top of the control?
Put the DataGridView in a Panel control and make the Panel scrollable. Make sure the DataGridView's Dock is set to none and the Height is set to the sum of the heights of the rows in the grid.
The basic answer is that you can't. The DataGridView paints itself by determining the current top row and then painting that top row into the upper left-hand corner, and then continuing on down (you can see this by using Reflector to look at the PaintRows method of the DataGridView).
Instead of trying to scroll the whole row, why not use an auto-sizing textbox? It will increase its height as the user types, and they can scroll the textbox itself.
have you checked "DataRepeater" control that comes under visual basic Power Packs 3.0; it can be very handy in your scenario.
Video Tutorial:
http://windowsclient.net/learn/video.aspx?v=30534
Download Link:
http://msdn.microsoft.com/en-us/vbasic/bb735936.aspx

Categories