want to set column width to the width of the datagridview

Hello Experts,

I have a datagridview. It populates with the records from the database. There are only four columns. I want the columns to fit into the length of the gridview. I tried AutosizeColumnsMode to 'Fill' but it is not working.

Thank you.
RadhaKrishnaKiJayaAsked:
Who is Participating?
 
RadhaKrishnaKiJayaConnect With a Mentor Author Commented:
Thank you vary much for your reply. But I found the solution.

Public Shared Sub Set_Grid_Width(myGridView As DataGridView)
        myGridView.ColumnHeadersDefaultCellStyle.WrapMode = False

        For i As Integer = 0 To myGridView.Columns.Count - 1
            If i = myGridView.Columns.Count - 2 Then
                myGridView.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            Else
                myGridView.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            End If
        Next i
    End Sub
End Class
0
 
sammySeltzerCommented:
I think that in addition to AutoSizeColumnsMode.Fill, you will also need to do it for all cells.

Something like:

AutosizeColumnsMode.AllCells
0
 
RadhaKrishnaKiJayaAuthor Commented:
I found the solution by myself.
0
All Courses

From novice to tech pro — start learning today.