Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I would like to set the form so that its size changes depending on the size of the DataGridView. After that, I would like to set the button to always be at the bottom of the form and at the same time in its middle.

What I have tried:

Does anyone have any advice on how to set it up?
C#
this.tabulkaAnotaci = new System.Windows.Forms.DataGridView();
            this.ID = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.tabulkaAnotaci)).BeginInit();
            this.SuspendLayout();
            // 
            // tabulkaAnotaci
            // 
            this.tabulkaAnotaci.AllowUserToDeleteRows = false;
            this.tabulkaAnotaci.AllowUserToResizeColumns = false;
            this.tabulkaAnotaci.AllowUserToResizeRows = false;
            this.tabulkaAnotaci.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.tabulkaAnotaci.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
            this.tabulkaAnotaci.BackgroundColor = System.Drawing.SystemColors.Menu;
            this.tabulkaAnotaci.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Raised;
            this.tabulkaAnotaci.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.tabulkaAnotaci.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.ID,
            this.Column1});
            this.tabulkaAnotaci.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabulkaAnotaci.Location = new System.Drawing.Point(0, 0);
            this.tabulkaAnotaci.MultiSelect = false;
            this.tabulkaAnotaci.Name = "tabulkaAnotaci";
            this.tabulkaAnotaci.ReadOnly = true;
            this.tabulkaAnotaci.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.tabulkaAnotaci.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.tabulkaAnotaci.ShowEditingIcon = false;
            this.tabulkaAnotaci.Size = new System.Drawing.Size(337, 364);
            this.tabulkaAnotaci.TabIndex = 0;
            this.tabulkaAnotaci.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.tabulkaAnotaci_CellClick);
            // 
            // ID
            // 
            this.ID.HeaderText = "ID";
            this.ID.Name = "ID";
            this.ID.ReadOnly = true;
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Jméno";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.button1.Location = new System.Drawing.Point(117, 329);
            this.button1.Name = "button1";
            this.button1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.button1.Size = new System.Drawing.Size(90, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "OK";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // FormularPresun
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.ClientSize = new System.Drawing.Size(337, 364);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.tabulkaAnotaci);
            this.Name = "FormularPresun";
            this.Text = "Bylo zvoleno více anotací";
            this.Load += new System.EventHandler(this.FormularPresun_Load);
            ((System.ComponentModel.ISupportInitialize)(this.tabulkaAnotaci)).EndInit();
            this.ResumeLayout(false);

I have it set up like this, but that's not it.

I will be glad for any advice.

Thank you David
Posted
Updated 30-Aug-22 21:23pm

1 solution

There is no automatic way to change the size of any container to fit it's content: you would have to do that yourself.

And it's gonna be messy: first you have to resize the DGV to fit the number of rows / columns it contains - and that's going to be really messy because the size is going to depend on the data in the cells, the type of data in the cells, the cell settings, the column settings, and the font used as well as the number of columns and rows involved.

Then you have to work out the actual size of the DGV including all headers and borders, then apply that to the size of the form, remembering to move all controls to the right and / or below the DGV in the form.

And then you get a messy result for the user, because unless you also keep a track of where the form is and the size / orientation of the actual monitor it is currently displayed on, your user is going to get a good chunk of the form off screen where he can't see it, and without any scroll bars to help him. And I run three monitors: one square, one portrait, and one landscape - so if I move your app from screen to screen the environment it runs in will change.

To be honest, it sounds like a heck of a lot of effort to go to to end up with a very poor UI experience for the user.
I'd strongly suggest you rethink the whole idea - while it's possible and sounds like it might be a nice gimmick, I'm pretty sure that in the real world, it's just annoy the heck out of people. There is a reason why we use scrollbars ...
 
Share this answer
 
Comments
dejf111 31-Aug-22 3:29am    
Thanks for the answer, it's quite interesting information for me, thanks. I'm just wondering if it's more logical if I roughly know how long the DGV will be and its maximum number of lines will be ten?
OriginalGriff 31-Aug-22 3:50am    
Or better, let the user control it via the Align and Dock properties - if he wants it bigger, he stretches the form.
That's what I do!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900