[c#] How to resize datagridview control when form resizes

I found a lot of questions about how to resize the form when a child control resizes, but I'm trying to do something much simpler (maybe so simple people don't even ask it :| )

I'd like to automatically resize my datagridview's width to fill the width of the form. After simply placing the datagridview on the form, the grid stays the same size when the form is resized. That happens even when I use the little constant-distance-snap thing 'connecting' the control to the form's borders.

Do I have to handle a form.resize event of some sorts, or is there a property I can set in VS?

This question is related to c# winforms datagridview resize

The answer is


You have to chose 'Fill' in the Dock property.


set the "Dock" property of datagridview in layoutto one of these properties : top, left, bottom, right. ok?


If you want to show the complete headers text

this will auto resize the columns so that the headers will show complete header text.

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

For Dock Mode

If you want to show the Dock Mode in your panel or form.

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

If anyone else is stuck with this, here's what helped me. Changing the Anchor settings did not work for me. I am using datagridviews within groupboxes in a form which is inside a parent form.

Handling the form resize event was the only thing that worked for me.

private void Form1_Resize(object sender, EventArgs e)
{
     groupBoxSampleQueue.MinimumSize = new Size((this as OperatingForm).Width - 22, 167);
     groupBoxMachineStatus.MinimumSize = new Size((this as OperatingForm).Width - 22, 167);
}

I added some raw numbers as buffers.


Set the property of your DataGridView:

Anchor: Top,Left
AutoSizeColumn: Fill
Dock: Fill

In your form constructor you could create an event handler like this:

this.SizeChanged(frm_sizeChanged);

Then create an event handler that resizes the grid appropriately, example:

private void frm_sizeChanged(object sender, EventArgs e)
{
     dataGrid.Size = new Size(100, 200);
}

Replacing those numbers with whatever you'd like.


The 'Anchor' property exists for any container: form, panel, group box, etc.

You can choose 1 side, left for example, or up to all four sides.

Anchor means the distance between the side(s) chosen and the edge of the container will stay the same, even upon resizing.

E.g., A datagridview, dgv1, is in the middle of Form1. Your 'Anchor' the left and top sides of dgv1. When the app is run and resizing occurs, either from different screen resolutions or changing the form size, the top and left sides of dgv1 will change accordingly to maintain their distance from the edge of From1. The bottom and right sides will not.


Set the anchor property of the control to hook to all sides of the parent - top, bottom, left, and right.


You have two options here:

  • Option one, Anchor
  • Option two, Dock

Look for both properties and figure out which one suit your needs.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.anchor.aspx

and

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock.aspx


Unless I am misunderstanding what you are asking you can do this on the properties for your data grid view. You need to set the Anchor property to the sides you want it locked to.


Use control anchoring. Set property Anchor of your GridView to Top, Left, Right and it will resize with container. If your GridView are placed inside of some container (ex Panel) then Panel should be anchored too.


For me, anchoring works only if I set it to all four sides:

Anchoring: Top, Bottom, Left, Right

Setting anchoring just to Left, Bottom moves the whole object when the form is resized in bottom, left side. Setting all four sizes really resizes the object, when parent is resized.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to datagridview

How to refresh or show immediately in datagridview after inserting? Delete a row in DataGridView Control in VB.NET Looping each row in datagridview How to get cell value from DataGridView in VB.Net? Changing datagridview cell color based on condition Index was out of range. Must be non-negative and less than the size of the collection parameter name:index how to bind datatable to datagridview in c# DataGridView AutoFit and Fill How to export dataGridView data Instantly to Excel on button click? Populate a datagridview with sql query results

Examples related to resize

Numpy Resize/Rescale Image AngularJS $watch window resize inside directive Input type "number" won't resize How can I make all images of different height and width the same via CSS? How to get full width in body element Rerender view on browser resize with React How to resize image automatically on browser width resize but keep same height? PHPExcel auto size column width Automatically resize images with browser size using CSS Change form size at runtime in C#