-4

I have bound some data to a DataGrid in a WinForm. Regardless of what I did on changing the size of the columns (like AutoSizeMode and DataGridViewAutoSizeColumnMode), I can't get my DataGrid to fill my WinForm, and when I resize the WinForm, the Datagrid size is fixed and is not resizing with the WinForm. Either I get a scrollbar if is the dataGrid is bigger than the form, or I have a huge space left after the last column.

I have got a result like in the picture below. Can anyone help to make the datagrid fit the form, and have it resize with the Form? Thank you.

DataGrid

This is not my DataGrid, just a picture from the internet, but it has the same result as mine.

Here is the code

enter link description here

PS: I haven't found a 100% solution yet, but a workaround, the problem was that the DataGrid source was a DataTable which had a DataTableStyle, and that one resize all the columns somehow (still couldn't figure out were exactly).So my workaround was that I added an Auto Align button in the menu, which reset the columns width by calculating the DataGridView width, and just devid it by the number of the columns in the DataTable and then it resizes each column.width in the DataTablestyle and voilà

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nizar Belhiba
  • 93
  • 1
  • 10
  • 2
    __Do not__ call a `DataGridView`a `GridView` or a `DataGrid` and vice versa!! This is wrong and confusing as those are different controls. Always call things by their __right__ name! - Have you set any Anchors of Docking? These are what changes a Control's Size. Or are you talking about the columns? Your question is ambiguous. – TaW Jun 14 '18 at 10:36
  • Just use the `Anchor` property on your control... – Nyerguds Jun 14 '18 at 10:39
  • I doubt just a picture from the internet is the same as your actual `datagridview`. And if it is really the same as the one shown above, then, even if you anchor the DGV with the actual size being smaller than the windows form(based on image above) its basically a given that it would not fill your `winform` assuming its the same on the image above that is not on `maximized` state. – P. Pat Jun 14 '18 at 10:50
  • Ok i'm sure that either one of us already answered your q. but if not take a look at this if your problem is resizing the DataGridView control with Form: https://stackoverflow.com/questions/15942682/resize-controls-when-form-resize or take a look at this if your problem is with columns not fitting into DataGridView: https://stackoverflow.com/questions/18666582/datagridview-autofit-and-fill. – eren Jun 14 '18 at 10:54
  • @TaW: Actually it's not a DataGridView but an user Control binded wit a Datagrid, and that user Control is embedded in a WinForm – Nizar Belhiba Jun 14 '18 at 11:43
  • @eren: believe me I tried all these – Nizar Belhiba Jun 14 '18 at 11:43
  • @NizarBelhiba Ok i do so show us your code please so we can figure out what's the problem. I for example 'till this moment don't know if the problem is that the DataGridView is not resizing with form or that the Columns within the DataGridView don't fill the grid. – eren Jun 14 '18 at 11:48
  • If the DGV is inside an UC __the same applies__: Use Docking and/or Anchors for __both__. (Try Anchors first, they are more versatile!) – TaW Jun 14 '18 at 12:01

3 Answers3

1

There is property on DataGridView called AutoSizeColumnMode make sure you set it to Fill if you want to reach desired effect (see the pic bellow). And with that also note that you need to set the AutoSizeMode to Fill for at least one Column. enter image description here

EDIT:

Ok i'll do an example for you. Let's say you've got WinForm application with one empty Form named Form1. Just copy paste the code bellow into your form and tell me if it works as you want:

        DataGridView dataGridView1;

        public Form1()
        {
            InitializeComponent();
            dataGridView1 = new System.Windows.Forms.DataGridView();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.Dock = DockStyle.Fill;
            // we will create two columns that will resize based on content within cells
            for (var i = 0; i < 2; i++)
            {
                var column = new DataGridViewTextBoxColumn()
                {
                    HeaderText = $"Column{i} - AllCells",
                    AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                };
                dataGridView1.Columns.Add(column);
            }
            // And one column that fill fill the rest of the grid 
            var fillColumn = new DataGridViewTextBoxColumn()
            {
                HeaderText = "FillColumn",
                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            };
            dataGridView1.Columns.Add(fillColumn);
            // so we've got grid with 3 columns let's fill some random data
            dataGridView1.Rows.Add("test", "test", "test test test");
            dataGridView1.Rows.Add("test", "test", "test test test");
            dataGridView1.Rows.Add("test", "test", "test test test");
            dataGridView1.Rows.Add("test", "test", "test test test");
            Controls.Add(dataGridView1);
        }
eren
  • 708
  • 8
  • 20
  • I did and it set to Fill – Nizar Belhiba Jun 14 '18 at 10:31
  • Did you set the property AutoSizeMode for each individual column? (If u want to reach the desired effect you need to set at least one of the columns AutoSizeMode property to Fill. – eren Jun 14 '18 at 10:33
  • @NizarBelhiba take a look at the code i posted, just copy paste it into Form1 within new WinForm Project and tell me if it works as you want – eren Jun 14 '18 at 12:09
  • @NizarBelhiba Well i took a look at your code, which one of those custom controls are you using? DataGridControl or DynamicColumnDataGridControl? – eren Jun 14 '18 at 12:35
  • DynamicColumnDataGridControl – Nizar Belhiba Jun 14 '18 at 13:18
  • @NizarBelhiba Ok i imported that DynamicColumnDataGridControl into my test project, populated with some random columns and everything works fine. Do you mind posting (or if you already posted tell me which file is it) the part of your code where your are populating that DynamicColumnDataGridControl with columns? – eren Jun 14 '18 at 13:52
  • @NizarBelhiba The problem may lie here. If you are using method: public void SetColumn (int column, string columnHeaderName) to populate the DynamicColumnDataGridControl. That method set the column AutoSizeMode to DataGridViewAutoSizeColumnMode.None by default. There is an overload for that method that uses 3rd argument: DataGridViewAutoSizeColumnMode. But i'm just guessing what may be the problem, the part from your code where you populate DynamicColumnDataGridControl instance with columns would really help. – eren Jun 14 '18 at 14:03
  • Sorry for my late reply the columns are set in DynamicColumnDataGridControl2 exactly under public void SetColumns(List columnHeaderNames, List columnDataTypes) – Nizar Belhiba Jun 15 '18 at 06:17
  • there he create a dataTable as Datasource for the DataGrid – Nizar Belhiba Jun 15 '18 at 06:21
  • Just for the Info, I found a workaround, the problem was that the DataGrid source was a DataTable which had a DataTableStyle, and that one resize all the columns somehow (still couldn't figure it out were exactly).So my workaround was that I added an Auto Align button in the menu, which reset the columns width by calculating the DataGridView width, and just devid it by the number of the columns in the DataTable and then it resize the coloumn.width in the DataTablestyle and voilà – Nizar Belhiba Jun 15 '18 at 13:59
1

The Anchor property will do the job. Just select all directions (Top, Bottom, Left, Right). You can do it by the designer or programmatically.

enter image description here

this.dataGridView1.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)));
L_J
  • 2,351
  • 10
  • 23
  • 28
  • 1
    Will this not achieve the same as setting dock=fill? – B.Hawkins Jun 14 '18 at 10:54
  • 1
    @B.Hawkins No, those are not same. If you use dock=fill it will occupy the whole form and you cannot have anything else on it. – L_J Jun 14 '18 at 10:59
  • @B.Hawkins to answer the orginal question, it's the fill property. But in general : They are not the same because if you have other controls in the form, you can't use Fill because it'll take the whole form. Then you need to add two panels and fill them, then fill one of them by dataGridView. – Kaj Jun 14 '18 at 11:00
  • The Anchor is set as you described. also I tried with Dock = Fill nothing it's really strange. The problem is that it's not me who programmed the whole thing, I'm just trying to fix that damn thing. – Nizar Belhiba Jun 14 '18 at 11:29
1

If you want the dataGridView to fill the Form, then there is a property in DataGridView named Dock. Set this property to Fill and the dataGridView will fill the Form completely, even when resizing, it'll be responsive with resizing. This will work for you if you want to fill the form.

But if you have another components in the form, then you have to use Anchor property. Since the dataGridView will be re-sizable with the Form without filling the form completely.

Kaj
  • 806
  • 6
  • 16
  • 1
    I'm not talking about the autoSizeColumnsMode ! The property name is : **Dock** – Kaj Jun 14 '18 at 11:34
  • Actually is not a dataGridView it's an usercontrol binded with a DataGrid – Nizar Belhiba Jun 14 '18 at 11:39
  • 1
    Then you have to dock both. Dock the userControl to form, then dock the dataGrid to user control. But any reason why not to use dataGridView ? – Kaj Jun 14 '18 at 11:40