Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resizing columns and sorting in an unbound DataGrid

Status
Not open for further replies.

VRIT

IS-IT--Management
Sep 2, 2002
60
GB
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
 
Thanks,

I have seen this website before and can only find ansers in C++, which is not what I want.

VRIT
 
There's lots of VB code there too.
You could take a look at an existing FAQ faq796-3899

Sweep
...if it works dont mess with it
 
Here's an example...

Dim objDataGridStyle As New DataGridTableStyle()
Dim objTextCol As New DataGridTextBoxColumn()

DataGrid1.TableStyles.Clear()

'The following code formats the datagrids column header and width
'Set the MappingName for the DataGridTableStyle
objDataGridStyle.MappingName = "ReturnData"

objTextCol.MappingName = "docid"
objTextCol.Width = 55
objTextCol.HeaderText = "Doc ID"
objDataGridStyle.GridColumnStyles.Add(objTextCol)


'Set references for columns
objTextCol = New DataGridTextBoxColumn()
objTextCol.MappingName = "tsstamp"
objTextCol.Width = 110
objTextCol.HeaderText = "Event Date"
objDataGridStyle.GridColumnStyles.Add(objTextCol)

objTextCol = New DataGridTextBoxColumn()
objTextCol.MappingName = "usrnam"
objTextCol.Width = 55
objTextCol.HeaderText = "Login ID"
objDataGridStyle.GridColumnStyles.Add(objTextCol)

'Add the DataGridTableStyle to the DataGrid
DataGrid1.TableStyles.Add(objDataGridStyle)
 
Hi VRIT. I had troubles too. We finally moved to Infragistics' DataGrid. It works just like VB6 in that it's easy to understand.
Someone else in the forum recomemded it to me. It rocks. M$'s grid stinks on ice. There's a lot they left out. You'd think that since vb3 they could learn to make a grid that did everything. :)
 
In total agreement with bigfoots comments.
I personally prefer Components One's DBGrid



Sweep
...if it works dont mess with it
 
Do you have a web addy for these alternative Datagrids

Thanks

VRIT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top