datagridview custom fill resize - c#

I have a DataGridView with a DataSource and I want the data to be displayed on the entire width of the control with the auto-computed width of each column. The exception is the first one which should always have 100px.
I handled the Size event, I set always the fist column to have 100px and it works, but only if I manually resize the grid.
Is there any way I can do this automatically, right after the DataSource was set?
I tried to invalidate the control, I set (again) the autofill (and not only) flags of the grid, but no luck. Did anyone managed to do a similar thing?

When editing the columns, there is a property called AutoSizeMode. Im wondering why you are trying to handle it yourself. Just set all columns except the first one to AutoSizeMode = AllCells and then give your first column the width of 100px. Should do it shouldnt it?
Bonus: You can even set one of the columns to Fill in AutoSizeMode. Would look a bit better as your table is taking the full width.
If I missed the point please correct me...

Related

In a C# DataGridView with enabled AutoSize, can I lock the width of certain columns?

I would like to simply enable autosizing on my datagridview (or similar behavior). I want some columns to grow and fill all available space. However, I also have some columns that I want 100% fixed and to not grow at all, and I can't get this particular behavior.
Unfortunately, no matter what the fill rate is, I can't figure out how to lock the columns I don't want to change. So far, I have tried:
Setting FillWeight to be extremely small on the columns I want locked. Unfortunately, this just causes the column to become sized too small off the bat, regardless of its initial width setting.
Setting Resizable = DataGridViewTriState.False only prevents the user from changing the size of that column. It still resizes automatically when I change the window size.
Does a solution exist? If I can't find one, I'll need to write up my own implementation, triggered using DataGridView.Resize I take it.
I see, MinimumWidth needs to be leveraged as part of the solution. FillWeight = float.Epsilon, MinimumWidth = 50 seems to do the trick.

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.

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. :)

Horizontal Scrollbar is not visible on DataGridView

I have a DataGridView on Window form which is populated with 30 columns and thousands of rows.
ScrollBars property is set to Both, but still horizontal scroll bar is not visible. even I am unable to scroll with arrow key from keyboard.
I tried it by setting ScrollBars property to Horizontal as well, but that does not make any difference.
Any suggestions please?
Thanks
Well Guys, its sorted out.
I am answering my own question; it may help someone in future.
one of the columns has Frozen property set as True. which should be false for all columns.
Now ScrollBar is working absolutely fine for me.
Cheers
I know this has already been resolved, but I've come across another reason why this could happen, so thought I'd add it as an answer in case someone else has the same problem.
If your form has a DataGridView that is docked to fill the form, and the form also has a status bar, then depending on the order they're created, the status bar can hide the DataGridView's scrollbar. To fix this, right click on the DataGridView, and select "Bring to Front".
When I encountered this annoying problem, it was due to the AutoSizeColumnsMode property of the DGV that was set to Fill
I fixed it by changing that property to AllCells, but any other value will work. It works even if the DGV is docked and I have multiple docked panels, and the first column is Frozen.
The docking.Fill of the DGV is a little buggy.
It happens when you have multiple docked panels, toolbars, etc.
More common when you're creating your columns at runtime.
The control thinks it is wider than its container and the Horizontal scrollbar does not spawn.
Frozen, autosize, brint to front and the other remedies mentioned don't always work.
The most reliable workaround is to Dock.Left and set the DGV's width at run time.
This way the DGV doesn't get confused about how wide it is.
i had the similar problem. What I did was, check each of your Datagrid columns and set the Frozen to "false". Hope that helps.
I was having this irritating problem. I had already created the DataGridView on my Form and am setting all the databinding and properties setting in the .CS file.
I just commented the line in my code behind file (.cs) i.e.
gvTblContent.AutoSize = true;
You need not to set the AutoSize property, horizontal and vertical scrollbars will be provided by default otherwise you can use :
gvTblContent.ScrollBars = ScrollBars.Both;
I had a similar problem, not due to the frozen property, but most likely to DataGridView bugs, after I added some rows programmatically. After reading this answer to another question I solved using:
dgv.PerformLayout()
after adding those rows, just before dgv was about to be shown.
In my case i just used ANCHOR Top, Bottom, Left, Right instead of DOCK Fill.
Try it.
I had a similar issue but inside SplitContainer. None of the above worked, but use all four anchors instead of Dock.Fill of DataGridView.
I also meet this problem. It's a stupid situation in my case.
Please check the position/size of DataGridView whether out of the form.
I had the same problem and found that my dataGridView was slightly larger than the form that it was in. I adjusted the size to fit in the Form and it worked!
Hope this helps!
Multiple show/hide columns on my side was causing the same issue. Had to add
dataGridView1.ScrollBars = ScrollBars.Both; after I process all the columns and rows in the datagridview
Nothing of the above helped before that
1. No Frozen Columns
2. Form load has dataGridView1.ScrollBars = ScrollBars.Both;
3. No Status bar
I too had this problem in VS2015 on a winform.
The winform has a table layout split into 4 rows, 1 column. Into the rows I place panels for placing other controls except the DataGrid row which is in the last row. The DataGrid is set with Dock to fill. The form also has a Status bar at the bottom, for future use.
What I found is that the status bar blocked the scrollbar as mentioned previously.
I added another row to the table layout but that would display a large blank space at the bottom of the form both at run time and design. Resizing the form did not resolve either. I tried setting the row height of the table layout but it did not work. I tried 1 pixel, 5 pixels etc. no change. In the end I gave up and removed the Status bar, wasn't using it for anything anyway.
I had a DataGridView sitting inside a cell of a TableLayoutPanel, and neither scroll bar was showing up on the DataGridView. I think the size of the DataGridView also wasn't being managed properly with the DataGridView being docked to fill a cell of the TableLayoutPanel. I didn't have any frozen columns.
I was able to fix it by placing the DataGridView inside a Panel, and setting AutoScroll=true on the Panel, to let the Panel manage the scrolling. I docked the panel to fill inside the cell of the TableLayoutPanel, and docked the DataGridView to fill inside of the Panel.
In my case Scroll bar didn't appear until when I realized the above frozen column state and read-only. I have done frozen columns for read-only column as well the one editable column in my dataGridView. When I remove frozen=false for editable column, the horizontal bar appear.
I set some first columns frozen to true it(H_bar) still works. But I set frozen = true to a invisible column (column.visible=false), it gone.
I had the same problem with DataGridView inside a tableLayoutPanel.
None of the above helped me.
It turns out that the column in the tableLayoutPanel in which the DataGridView is is set to AutoSize.
The solution is to set the tableLayoutPanel columns to actual value or percent.
All your frozen columns should fit within the form.
Otherwise horizontal Scrollbar does not show up.
If there is no horizontal Scrollbar then make your form wider until you see your last frozen field and then Horizontal Scrollbar will magically appear.
There one more thing you could check on. The "Enabled" property of the GridView control should be set at "true".
DataGridView Properties:
Fixed my problem, but it was none of the above.
In my situation, I was creating my entire form at run-time. Each DataGridView was Dock.Fill inside a TableLayoutPanel. You don't have to specify row or columns for a TableLayoutPanel if there is only going to be one, and it should be 100%. Doing this however breaks DataGridView. Fixed my problem by simply adding the row 100% definition.

Show/Hide for cells of TableLayout

C# Winforms:
My tableLayout has only one column but it has three rows. I want to be able to show/hide the rows. I did a google search and found this, it works perfect for Hiding the row But what to do for showing it again? setting the height to a hard coded number? Not a good idea ...
tableLayoutPanel1.RowStyles[0].SizeType = SizeType.Absolute;
tableLayoutPanel1.RowStyles[0].Height = 0;
what do you propose to accomplish this?
I would argue that you should set the .Visible property for each control you want to show/hide. Presumably at least one row and column are set to 100% (they absorb the extra space) - that row/column will resize when those controls are no longer visible. Hope that helps.

Categories