• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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