DataGridView - how to set column width?

后端 未结 13 2271
情书的邮戳
情书的邮戳 2020-12-13 23:58

I have a WinForms application with DataGridView control. My control has five columns (say \"Name\", \"Address\", \"Phone\" etc)

I am not happy with defa

相关标签:
13条回答
  • 2020-12-14 00:47

    Use the Columns Property and set the Auto Size Mode to All Cells, Resizable to True, Frozen to False and visible to True.

    The column will automatically resize based on the data inserted.

    0 讨论(0)
  • 2020-12-14 00:48

    i suggest to give a width to all the grid's columns like this :

    DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
    col.HeaderText = "phone"            
    col.Width = 120;
    col.DataPropertyName = (if you use a datasource)
    thegrid.Columns.Add(col);    
    

    and for the main(or the longest) column(let's say address) do this :

    col = new DataGridViewTextBoxColumn();
    col.HeaderText = "address";
    col.Width = 120;
    

    tricky part

    col.MinimumWidth = 120;
    col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
    

    tricky part

    col.DataPropertyName = (same as above)
    thegrid.Columns.Add(col);
    

    In this way, if you stretch the form (and the grid is "dock filled" in his container) the main column, in this case the address column, takes all the space available, but it never goes less than col.MinimumWidth, so it's the only one that is resized.

    I use it, when i have a grid and its last column is used for display an image (like icon detail or icon delete..) and it doesn't have the header and it has to be always the smallest one.

    0 讨论(0)
  • 2020-12-14 00:51

    You can set default width and height for all Columns and Rows as below only with one loop.

        // DataGridView Name= dgvMachineStatus
    
       foreach (DataGridViewColumn column in dgvMachineStatus.Columns)
            {
                column.Width = 155;
            }
    
       foreach (DataGridViewRow row in dgvMachineStatus.Rows)
            {
                row.Height = 45;
            }
    
    0 讨论(0)
  • 2020-12-14 00:53

    You can use the DataGridViewColumn.Width property to do it:

    DataGridViewColumn column = dataGridView.Columns[0];
    column.Width = 60;
    

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.width.aspx

    0 讨论(0)
  • 2020-12-14 00:54

    Most of the above solutions assume that the parent DateGridView has .AutoSizeMode not equal to Fill. If you set the .AutoSizeMode for the grid to be Fill, you need to set the AutoSizeMode for each column to be None if you want to fix a particular column width (and let the other columns Fill). I found a weird MS exception regarding a null object if you change a Column Width and the .AutoSizeMode is not None first.

    This works

    chart.AutoSizeMode  = DataGridViewAutoSizeColumnMode.Fill;
    ... add some columns here
    chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    chart.Column[i].Width = 60;
    

    This throws a null exception regarding some internal object regarding setting border thickness.

    chart.AutoSizeMode  = DataGridViewAutoSizeColumnMode.Fill;
    ... add some columns here
     // chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    chart.Column[i].Width = 60;
    
    0 讨论(0)
  • 2020-12-14 00:54
    public static void ArrangeGrid(DataGridView Grid)
    { 
        int twidth=0;
        if (Grid.Rows.Count > 0)
        {
            twidth = (Grid.Width * Grid.Columns.Count) / 100;
            for (int i = 0; i < Grid.Columns.Count; i++)
                {
                Grid.Columns[i].Width = twidth;
                }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题