• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

Setting width of a datatable in vb.net 2005

I need to specify the width of the columns so the user can see all of the data without having to resize the column.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt.Columns.Add("FileName", GetType(String))
        dt.Columns.Add("Extension", GetType(String))
        dt.Columns.Add("Directory", GetType(String))
        dt.Columns.Add("Modified Date", GetType(DateTime))
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dt.Rows.Clear()
        Dim strDir As String
        strDir = "directory"
        DirAndFilesViewer(New System.IO.DirectoryInfo(strDir))
        DataGridView1.DataSource = dt
    End Sub
    Sub DirAndFilesViewer(ByVal di As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In di.GetFiles
            Dim targetDate As Date
            If LastWeek.Checked Then
                targetDate = Date.Today.AddDays(-7)
                If fi.LastWriteTime >= targetDate Then
                    If UCase(fi.Extension) = ".TXT" Or UCase(fi.Extension) = ".ASP" Or UCase(fi.Extension) = ".ASPX" Or UCase(fi.Extension) = ".HTM" Or UCase(fi.Extension) = ".HTML" Then
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
                    End If
                End If
            End If
            If LastDay.Checked Then
                targetDate = Date.Today.AddDays(-1)
                If fi.LastWriteTime >= targetDate Then
                    If UCase(fi.Extension) = ".TXT" Or UCase(fi.Extension) = ".ASP" Or UCase(fi.Extension) = ".ASPX" Or UCase(fi.Extension) = ".HTM" Or UCase(fi.Extension) = ".HTML" Then
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
                    End If
                End If
            End If
        Next

        For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
            DirAndFilesViewer(subDi)
        Next
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
0
-cr-
Asked:
-cr-
1 Solution
 
VBRocksCommented:
Just set the AutoSizeColumnsMode property as below:

        Me.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

0
 
-cr-Author Commented:
Doh!! That was easy! Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now