Groups | Blog | Home
all groups > vb.net data > january 2005 >

vb.net data : VB6 Grid to MS.NET grid (DataGrid Control)



Viet
1/4/2005 12:34:03 PM
I am unfamiliar with the MS.NET datagrid control so I am hoping someone
could convet the following VB6 grid control code to the MS Datagrid control.
I understand that the Datagrid control is implemented very differenly than
the VB6 grid control (such as the labeling of columns, column widths etc) so
I need help in converting the following.
Thanks!
Jonathan

Code:
'Refresh the RemGrid control and load up data
RemGrid1.Refresh()
RemGrid1.DataSource = Nothing
RemGrid1.DataSource = rsRemittance

Do Until RemGrid1.Columns.Count = rsRemittance.Fields.Count
RemGrid1.Refresh()
RemGrid1.DataSource = rsRemittance
RemGrid1.Refresh()
Loop

'Display the grid columns and data
RemGrid1.ColumnHeaders = True
RemGrid1.Columns(0).Width = 1
RemGrid1.Columns(0).Caption = "ID"
RemGrid1.Columns(1).Width = 100
RemGrid1.Columns(1).Caption = "Type"
RemGrid1.Columns(2).Width = 185
RemGrid1.Columns(2).Caption = "Identifier"
RemGrid1.Columns(3).Width = 90
RemGrid1.Columns(3).Caption = "Amount"
RemGrid1.Columns(3).Alignment =
MSDataGridLib.AlignmentConstants.dbgRight
RemGrid1.Columns(4).Visible = False
RemGrid1.Columns(5).Visible = False
RemGrid1.Columns(6).Visible = False
RemGrid1.Columns(7).Visible = False
RemGrid1.Columns(8).Visible = False
RemGrid1.Columns(9).Visible = False
RemGrid1.Columns(10).Visible = False

'Once the grid data extends past 6 rows, then create the scrollbar
and scroll down
TryAgain:

If rsRemittance.RecordCount > 6 Then
RemGrid.Height = 148
Else
Try
RemGrid.Height = 23 + (21 * rsRemittance.RecordCount)
Catch
GoTo TryAgain
End Try
End If


I converted the above code to the following:

Code:
Add a dataGrid on the form DataGrid1

At Form Level Declare a dataTable
Dim DT as DataTable=New DataTable()

Now on Form Load or on click of a Button

Dim DA As OleDbDataAdapter = New OleDbDataAdapter("Select * From
Mytable", "YourConnectionString")
DA.Fill(DT)
DataGrid1.DataSource = DT
FormatGrid()


Now write the function FormatGrid to Hide a few columns that you don't want
displayed

Private Sub FormatGrid()
Dim TS As New DataGridTableStyle
TS.MappingName = "MyTable"

Dim Col As New DataGridTextBoxColumn
Col.MappingName = "ID" 'Actual Column Name
Col.HeaderText = "ID"
Col.Width = 100
TS.GridColumnStyles.Add(Col)

'For each column you need to set the formatting like this

Col = New DataGridTextBoxColumn
Col.MappingName = "Type"
Col.HeaderText = "Type"
Col.Width = 100
TS.GridColumnStyles.Add(Col)

Col = New DataGridTextBoxColumn
Col.MappingName = "Identifier"
Col.HeaderText = "Identifier"
Col.Width = 100
TS.GridColumnStyles.Add(Col)

Col = New DataGridTextBoxColumn
Col.MappingName = "Amount"
Col.HeaderText = "Amount"
Col.Width = 50
Col.Format = "C"
Col.Alignment = HorizontalAlignment.Right
TS.GridColumnStyles.Add(Col)

Col = New DataGridTextBoxColumn
Col.MappingName = "Next Column"
Col.HeaderText = ""
Col.Width = 0 '0 Width will hide the column
TS.GridColumnStyles.Add(Col)

Col = New DataGridTextBoxColumn
Col.MappingName = "Next Column"
Col.HeaderText = ""
Col.Width = 0 '0 Width will hide the column
TS.GridColumnStyles.Add(Col)
'Similarly for all columns

'Now add the formatting to the Datagrid
DataGrid1.TableStyles.Add(TS)


End Sub

Is this correct?? Also, the Datasource points to a XML file as a
ADODB.recordset file so how do I specifiy the Datasource to this file?
Thanks!

Viet
1/5/2005 4:28:28 PM
Nevermind...got it converted....


[quoted text, click to view]

AddThis Social Bookmark Button