Results 1 to 2 of 2

Thread: Usage of DGV

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Usage of DGV

    This is the first time i am using an DGV and also an DB. I have written some code , but i am not sure whether it is the correct way of doing, plz help.

    Basically i want to load the data from the db and display in the dgv. I only want to display the data , manipulation of this data is done using other form.

    I am setting the columns manually because i want to use the same dgv for multiple purpose

    vb Code:
    1. Private Sub LoadDivisions()
    2.         Using conn As New SqlConnection(My.Settings.IDSConnectionString)
    3.             Using cmd As New SqlCommand("select * from divisions order by divname", conn)
    4.                 If Not conn.State = ConnectionState.Open Then conn.Open()
    5.  
    6.                 Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    7.                 Dim table As New DataTable
    8.                 table.Load(reader)
    9.  
    10.                 With Me.DataGridView1
    11.                     .Rows.Clear()
    12.                     .Columns.Clear()
    13.                     .ColumnCount = 0
    14.  
    15.                     .Columns.Add("Sno", "S.No.")
    16.                     .Columns.Add("DivId", "DivId")
    17.                     .Columns.Add("DivName", "Division Name")
    18.                     .Columns.Add("NOP", "No. of Products")
    19.                     .Columns.Add("Speciality", "Speciality")
    20.                     .Columns.Add("Descp", "Description")
    21.  
    22.                     .Columns(0).Width = 50
    23.                     .Columns(1).Visible = False
    24.                     .Columns(2).Width = 225
    25.                     .Columns(3).Width = 115
    26.                     .Columns(4).Width = 150
    27.                     .Columns(5).Width = 310
    28.  
    29.                     For Each r As DataRow In table.Rows
    30.                         .Rows.Add(.Rows.Count + 1, r(0), r(1), "", r(2), r(3))
    31.  
    32.                     Next
    33.                 End With
    34.  
    35.            End Using
    36.         End Using
    37.     End Sub

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Usage of DGV

    There are a few different ways you can use the DGV - your way is one of them but I think the most common method is to use Data Binding. I've never really got into that myself though and always done something similar to what you have there but its certainly worth looking into (do some searching on google and on this forum for something like "vb.net datagridview data binding" )
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width