Hi,
I seem to be having alot of trouble with DataGrids since I moved to VB .Net. I have included the code I have written, be it good or not. What I a have is an unbound grid that can should be sorted via various radio buttons. I also dont want the user to be able to alter the column widths and i want to set them myself.
Any help most welcome:
Sub Load_UserDetails()
'ToDo: Add details to DataGrid (DGUserDetails)
Dim UserSQL As Object
Dim cnConn As ADODB.Connection
Dim UserRS As ADODB.Recordset
Dim objDS As New DataSet
'Create data table.
Dim DTUserDetails As New DataTable("myTable"
'Clear the DataGrid
'DTUserDetails.Clear()
DGUserDetails.TableStyles.Clear()
'Create columns.
Dim ColForename As New DataColumn
Dim ColSurname As New DataColumn
Dim ColVillage As New DataColumn
Dim ColContactDetails As New DataColumn
Dim ColComputerName As New DataColumn
Dim ColAssetNumber As New DataColumn
'Add columns to the data table.
DTUserDetails.Columns.Add(ColForename)
DTUserDetails.Columns.Add(ColSurname)
DTUserDetails.Columns.Add(ColVillage)
DTUserDetails.Columns.Add(ColContactDetails)
DTUserDetails.Columns.Add(ColComputerName)
DTUserDetails.Columns.Add(ColAssetNumber)
ColForename.ColumnName = "Forename"
ColSurname.ColumnName = "Surname"
ColVillage.ColumnName = "Village"
ColContactDetails.ColumnName = "Contact Details"
ColComputerName.ColumnName = "Computer Name"
ColAssetNumber.ColumnName = "Asset Number"
'Create data row.
Dim myDataRow As DataRow
cnConn = New ADODB.Connection
UserRS = New ADODB.Recordset
With cnConn
.Provider = "SQLOLEDB.1"
.Properties("Data Source".Value = "DEVSQP"
.Properties("User ID".Value = SQLUN
.Properties("Password".Value = SQLPW
.Properties("Initial Catalog".Value = "VRS"
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.ConnectionTimeout = 0
.Open()
End With
UserSQL = "SELECT * FROM UserDetails ORDER BY '" & SortGridBy & "'"
UserRS.Open(UserSQL, cnConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
If Not UserRS.EOF Then
UserRS.MoveFirst()
Do While Not UserRS.EOF
myDataRow = DTUserDetails.NewRow
myDataRow(0) = Trim(UserRS.Fields("Forename".Value)
myDataRow(1) = Trim(UserRS.Fields("Surname".Value)
myDataRow(2) = Trim(UserRS.Fields("Village".Value)
myDataRow(3) = Trim(UserRS.Fields("Contact_Details".Value)
myDataRow(4) = Trim(UserRS.Fields("Computer_Name".Value)
myDataRow(5) = Trim(UserRS.Fields("Asset_Number".Value)
DTUserDetails.Rows.Add(myDataRow)
UserRS.MoveNext()
Loop
UserRS.Close()
cnConn.Close()
End If
'Add data table to the DataSet.
objDS.Tables.Add(DTUserDetails)
'Bind DataSet to the DataGrid.
DGUserDetails.SetDataBinding(objDS, "myTable"
End Sub
I seem to be having alot of trouble with DataGrids since I moved to VB .Net. I have included the code I have written, be it good or not. What I a have is an unbound grid that can should be sorted via various radio buttons. I also dont want the user to be able to alter the column widths and i want to set them myself.
Any help most welcome:
Sub Load_UserDetails()
'ToDo: Add details to DataGrid (DGUserDetails)
Dim UserSQL As Object
Dim cnConn As ADODB.Connection
Dim UserRS As ADODB.Recordset
Dim objDS As New DataSet
'Create data table.
Dim DTUserDetails As New DataTable("myTable"
'Clear the DataGrid
'DTUserDetails.Clear()
DGUserDetails.TableStyles.Clear()
'Create columns.
Dim ColForename As New DataColumn
Dim ColSurname As New DataColumn
Dim ColVillage As New DataColumn
Dim ColContactDetails As New DataColumn
Dim ColComputerName As New DataColumn
Dim ColAssetNumber As New DataColumn
'Add columns to the data table.
DTUserDetails.Columns.Add(ColForename)
DTUserDetails.Columns.Add(ColSurname)
DTUserDetails.Columns.Add(ColVillage)
DTUserDetails.Columns.Add(ColContactDetails)
DTUserDetails.Columns.Add(ColComputerName)
DTUserDetails.Columns.Add(ColAssetNumber)
ColForename.ColumnName = "Forename"
ColSurname.ColumnName = "Surname"
ColVillage.ColumnName = "Village"
ColContactDetails.ColumnName = "Contact Details"
ColComputerName.ColumnName = "Computer Name"
ColAssetNumber.ColumnName = "Asset Number"
'Create data row.
Dim myDataRow As DataRow
cnConn = New ADODB.Connection
UserRS = New ADODB.Recordset
With cnConn
.Provider = "SQLOLEDB.1"
.Properties("Data Source".Value = "DEVSQP"
.Properties("User ID".Value = SQLUN
.Properties("Password".Value = SQLPW
.Properties("Initial Catalog".Value = "VRS"
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.ConnectionTimeout = 0
.Open()
End With
UserSQL = "SELECT * FROM UserDetails ORDER BY '" & SortGridBy & "'"
UserRS.Open(UserSQL, cnConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)
If Not UserRS.EOF Then
UserRS.MoveFirst()
Do While Not UserRS.EOF
myDataRow = DTUserDetails.NewRow
myDataRow(0) = Trim(UserRS.Fields("Forename".Value)
myDataRow(1) = Trim(UserRS.Fields("Surname".Value)
myDataRow(2) = Trim(UserRS.Fields("Village".Value)
myDataRow(3) = Trim(UserRS.Fields("Contact_Details".Value)
myDataRow(4) = Trim(UserRS.Fields("Computer_Name".Value)
myDataRow(5) = Trim(UserRS.Fields("Asset_Number".Value)
DTUserDetails.Rows.Add(myDataRow)
UserRS.MoveNext()
Loop
UserRS.Close()
cnConn.Close()
End If
'Add data table to the DataSet.
objDS.Tables.Add(DTUserDetails)
'Bind DataSet to the DataGrid.
DGUserDetails.SetDataBinding(objDS, "myTable"
End Sub