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:
Private Sub LoadDivisions() Using conn As New SqlConnection(My.Settings.IDSConnectionString) Using cmd As New SqlCommand("select * from divisions order by divname", conn) If Not conn.State = ConnectionState.Open Then conn.Open() Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) Dim table As New DataTable table.Load(reader) With Me.DataGridView1 .Rows.Clear() .Columns.Clear() .ColumnCount = 0 .Columns.Add("Sno", "S.No.") .Columns.Add("DivId", "DivId") .Columns.Add("DivName", "Division Name") .Columns.Add("NOP", "No. of Products") .Columns.Add("Speciality", "Speciality") .Columns.Add("Descp", "Description") .Columns(0).Width = 50 .Columns(1).Visible = False .Columns(2).Width = 225 .Columns(3).Width = 115 .Columns(4).Width = 150 .Columns(5).Width = 310 For Each r As DataRow In table.Rows .Rows.Add(.Rows.Count + 1, r(0), r(1), "", r(2), r(3)) Next End With End Using End Using End Sub





Reply With Quote