none
How to adjust the column width manually after set the AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells RRS feed

  • Question

  • I set the AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells when the form size max, but I can not adjust the column width manually using mouse.

      Private Sub form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
                If Me.MdiParent.WindowState = FormWindowState.Maximized Then
                        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells          
                End If
      End Sub

    Sunday, August 27, 2017 12:19 PM

Answers

  • Hi gaxjyxq,

    Based on your description, you want to set AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCells, then you also can adjust column width manually using mouse. I think there is no possible, because the column widths adjust to fit the contents of all cells in the columns, the column width is fix.

    If you set AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.fill, you can use mouse to adjust the column width manually.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewautosizecolumnsmode(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gaxjyxq Monday, August 28, 2017 11:28 AM
    Monday, August 28, 2017 6:33 AM
    Moderator

All replies

  • I set the AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells when the form size max, but I can not adjust the column width manually using mouse.

      Private Sub form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
                If Me.MdiParent.WindowState = FormWindowState.Maximized Then
                        DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells          
                End If
      End Sub

    Hi

    You are mixing apples and pears = mismatch.

    Give this a try:  it scales to fit width using one chosen column (you could choose as many as needed).

        Private Sub form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            If Me.WindowState = FormWindowState.Maximized Then
                With DataGridView1
                    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
    
                    ' choose a column to scale to fill width
                    .Columns("Column3").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                End With
            Else
                With DataGridView1
                    .Columns("Column3").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                End With
            End If
        End Sub


    Regards Les, Livingston, Scotland

    Sunday, August 27, 2017 12:47 PM
  • Hi gaxjyxq,

    Based on your description, you want to set AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.AllCells, then you also can adjust column width manually using mouse. I think there is no possible, because the column widths adjust to fit the contents of all cells in the columns, the column width is fix.

    If you set AutoSizeColumnsMode=DataGridViewAutoSizeColumnsMode.fill, you can use mouse to adjust the column width manually.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewautosizecolumnsmode(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gaxjyxq Monday, August 28, 2017 11:28 AM
    Monday, August 28, 2017 6:33 AM
    Moderator