Home All Groups Group Topic Archive Search About

One line of code not working : susposed to resize datagrid column..

Author
5 Apr 2006 9:59 AM
garyusenet
I'm following a sql/vb tutorial which contains the following code which
is meant to resize the datagrid view so that the columns take the
correct width for the content within them.

It doesn't work the syntax seems to be nonsense according to VS2005 so
can someone suggest what I need to replace it with?


Thanks...


dgvStatus.AutoSizeColumns
(DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows)

p.s. dgvStatus is the name of a datagrid control i have on the form.

Author
5 Apr 2006 11:37 AM
Ken Tucker [MVP]
Hi,

        Use this to make the column resize its width.

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim strConn As String = _
            "Server = .\SQLEXPRESS;Database = NorthWind; Integrated Security
= SSPI;"
        Dim conn As New SqlConnection(strConn)
        Dim da As New SqlDataAdapter("Select * from Categories", conn)
        Dim dt As New DataTable

        da.Fill(dt)
        DataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.AllCells
        DataGridView1.DataSource = dt

        With DataGridView1.Columns("Description")
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        End With

        With DataGridView1.Columns("Image")
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        End With
    End Sub


End Class

This will make the column multi line

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim strConn As String = _
            "Server = .\SQLEXPRESS;Database = NorthWind; Integrated Security
= SSPI;"
        Dim conn As New SqlConnection(strConn)
        Dim da As New SqlDataAdapter("Select * from Categories", conn)
        Dim dt As New DataTable

        da.Fill(dt)
        DataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.AllCells
        DataGridView1.DataSource = dt

        With DataGridView1.Columns("Description")
            .Width = 100
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .DefaultCellStyle.WrapMode = DataGridViewTriState.True
        End With

        With DataGridView1.Columns("Image")
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        End With
    End Sub


End Class


Ken
----------------
<garyuse***@myway.com> wrote in message
Show quoteHide quote
news:1144231195.718170.272110@e56g2000cwe.googlegroups.com...
> I'm following a sql/vb tutorial which contains the following code which
> is meant to resize the datagrid view so that the columns take the
> correct width for the content within them.
>
> It doesn't work the syntax seems to be nonsense according to VS2005 so
> can someone suggest what I need to replace it with?
>
>
> Thanks...
>
>
> dgvStatus.AutoSizeColumns
> (DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows)
>
> p.s. dgvStatus is the name of a datagrid control i have on the form.
>