How to handle an empty datagridview? - c#

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.

Related

Scrolling with multiple controls in panel 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.

WPF how to reset grid row and column sizes to "*" in code

I have a grid that contains several controls in rows of a grid. I have the RowDefinitions set so that each row in the grid has a height of "*" in the XAML, so that all the controls have the same amount of room available to them.
I have a configuration screen that allows the user to disable/enable some of the controls. If a control is disabled, I change the Visibility to Collapsed and set its row height in the grid to zero. This works great on application start up.
The issue is that if the user goes into the settings screen and re-enables controls (that were previously hidden) while the application is already running, I'm not sure how to set the control's grid column back to the correct size, and make it re-distribute all the available height among the new (increased) number of rows.
Is there a way to set all the heights back to "*" in C# code, and have WPF do the redistribution?
If not, any other ideas, other than doing the math myself and manually setting the row heights, or restarting the whole application?
You can set the star values like this:
rowDef.Height = new GridLength(1, GridUnitType.Star);
See also: http://msdn.microsoft.com/en-us/library/system.windows.gridunittype.aspx

Customize WPF application user controls

I'm trying to put in my windows a Datagrid and some other things (TextBox, Dropbox).
My problem is that when I resize the windows I can't control the items.
What I want to do is to devide the Windows like the picture below, and want the them to fit the window when I resize the window.
What do you suggest.
Put them in a Grid with two RowDefinitions. One for the toolbar/controls at the top, the other for the DataGrid.
Make the first row height Auto, and the second row height *.
Set the HorizontalAlignment and VerticalAlignment properties on the DataGrid to Stretch.
You should definitely use the Grid class.
Add Rows and Columns and change the Height and Width properties of these object to resize as you want (fixed value, Auto, *).

C# Datagridview set column width to prefered size when datagrid is on fill

I have a datagridview, that is set to "Fill" because that way you have no ugly blank spots. Now I want to resize 2 columns so the content is fully shown.
With this piece of code I can change the width of one column:
DataGrid.Columns[1].Width = DataGrid.Columns[1].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
But when I add it for the other cell that needs to be resized
DataGrid.Columns[5].Width = DataGrid.Columns[5].GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, true);
It overrides the first one, so only the last cell will be resized correctly.
Does anyone have an idea on how to fix this?
Perform this in the parent's (Form's) Load event and not the constructor.
Even if the column's auto size mode is set to Fill, the columns' resulting values won't be set until it loads.
Let me know if I should elaborate. :)

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