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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Datagrid Question

Status
Not open for further replies.

sanders720

Programmer
Aug 2, 2001
421
0
0
US
Simple Question, how do I get my data in the datagrid? Full code below. This data is not derived from a database, but properties from our CAD package, which will eventually be synchronized with data. For now, I am trying to firgue out how to manipulate the CAD data. However, is there a strategy to pull a conventional data source into the same datagrid?

So, question 1 - how can I populate the data, and

question 2 - can I pull the data from a different source into the same datagrid?

Thanks in advance for the help!



' Add Rows
Dim dr As DataRow
dr("id") = 0
dr("Item") = 0.ToString(PN, RPN, L, RSP, PDesc, MFGBY, REV, MFGCODE, MACODE, MAT, FIN, DES, RENMATP)
dt.Rows.Add(dr)

DataGrid1.DataSource = dt
DataGrid1.AllowSorting = True





Public Sub PopulateDataGrid1()
' Add Column Data

' Part No.
Dim PN As String
If Len(oProp3(2).Value + "") > 0 Then
PN = oProp3(2).Value
Else
PN = ""
End If

' Reference Part No.
Dim RPN As String
If Len(oProp4(7).Value + "") > 0 Then
RPN = oProp4(7).Value
Else
RPN = ""
End If

' Length
Dim L As Single
If Len(oProp4(8).Value) > 0 Then
L = oProp4(8).Value
Else
L = 0
End If

' Recommended Spare Part
Dim RSP As Boolean
If Len(oProp4(6).Value) > 0 Then
RSP = oProp4(6).Value
Else
RSP = False
End If

' Part Description
Dim PDesc As String
If (Len(oProp3(14).Value + "") > 0) Or (Len(oProp3(3).Value + "") > 0) Then
PDesc = oProp3(14).Value & " " & oProp3(3).Value
Else
PDesc = ""
End If


' Manufactured By
Dim MFGBY As String
If Len(oProp3(15).Value + "") > 0 Then
MFGBY = oProp3(15).Value
Else
MFGBY = ""
End If

' Revision
Dim REV As String
If Len(oProp1(7).Value + "") > 0 Then
REV = oProp1(7).Value
Else
REV = ""
End If

' Manufacturer Code
Dim MFGCODE As String
If Len(oProp4(5).Value + "") > 0 Then
MFGCODE = oProp4(5).Value
Else
MFGCODE = ""
End If

' Material Code
Dim MACODE As String
If Len(oProp4(2).Value + "") > 0 Then
MACODE = oProp4(2).Value
Else
MACODE = ""
End If

' Material Description
Dim MAT As String
If Len(oProp4(3).Value + "") > 0 Then
MAT = oProp4(3).Value
Else
MAT = ""
End If

' Part Finish
Dim FIN As String
If Len(oProp4(1).Value + "") > 0 Then
FIN = oProp4(1).Value
Else
FIN = ""
End If

' Designer
Dim DES As String
If Len(oProp1(3).Value + "") > 0 Then
DES = oProp1(3).Value
Else
DES = ""
End If

' Rendered Material / Property
Dim RENMATP As String
If Len(oProp3(10).Value + "") > 0 Then
RENMATP = oProp3(10).Value
Else
RENMATP = ""
End If

' Add Columns
Dim tableStyle As New DataGridTableStyle

' Part No.
Dim column As New DataGridTextBoxColumn
column.MappingName = "pn"
column.HeaderText = "Part Number"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Reference Part No.
column = New DataGridTextBoxColumn
column.MappingName = "rpn"
column.HeaderText = "Reference Part Number"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Length
column = New DataGridTextBoxColumn
column.MappingName = "l"
column.HeaderText = "Length"
column.Width = 13
tableStyle.GridColumnStyles.Add(column)

' Recommended Spare Part
column = New DataGridTextBoxColumn
column.MappingName = "rsp"
column.HeaderText = "RSP"
column.Width = 7
tableStyle.GridColumnStyles.Add(column)

' Part Description
column = New DataGridTextBoxColumn
column.MappingName = "pdesc"
column.HeaderText = "Part Description"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Manufactured By
column = New DataGridTextBoxColumn
column.MappingName = "mfgby"
column.HeaderText = "Manufactured By"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Revision
column = New DataGridTextBoxColumn
column.MappingName = "rev"
column.HeaderText = "Revision"
column.Width = 7
tableStyle.GridColumnStyles.Add(column)

' Manufacturer Code
column = New DataGridTextBoxColumn
column.MappingName = "mfgcode"
column.HeaderText = "Manufacturer Code"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Material Code
column = New DataGridTextBoxColumn
column.MappingName = "macode"
column.HeaderText = "Material Code"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Material Description
column = New DataGridTextBoxColumn
column.MappingName = "mat"
column.HeaderText = "Material Description"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Part Finish
column = New DataGridTextBoxColumn
column.MappingName = "fin"
column.HeaderText = "Part Finish"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Designer
column = New DataGridTextBoxColumn
column.MappingName = "des"
column.HeaderText = "Designer"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

' Rendered Material / Property
column = New DataGridTextBoxColumn
column.MappingName = "renmatp"
column.HeaderText = "Material Property"
column.Width = 25
tableStyle.GridColumnStyles.Add(column)

tableStyle.MappingName = "MetroParts"
DataGrid1.TableStyles.Add(tableStyle)

' Define Data Table
Dim dt As DataTable = New DataTable

Dim dc As DataColumn

' Add Columns
'Part Number
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "pn"
dc.AutoIncrement = False
dc.Caption = "Part Number"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Reference Part No.
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "rpn"
dc.AutoIncrement = False
dc.Caption = "Reference Part Number"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Length
dc = New DataColumn
dc.DataType = System.Type.GetType("System.Single")
dc.ColumnName = "l"
dc.AutoIncrement = False
dc.Caption = "Length"
dc.ReadOnly = False
dc.Unique = False

' Recommended Spare Part
dc = New DataColumn
dc.DataType = System.Type.GetType("System.Boolean")
dc.ColumnName = "rsp"
dc.AutoIncrement = False
dc.Caption = "Length"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Part Description
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "pdesc"
dc.AutoIncrement = False
dc.Caption = "Part Description"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Manufactured By
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "mfgby"
dc.AutoIncrement = False
dc.Caption = "Manufactured By"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Revision
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "rev"
dc.AutoIncrement = False
dc.Caption = "Revision"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Manufacturer Code
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "mfgcode"
dc.AutoIncrement = False
dc.Caption = "Manufacturer Code"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Material Code
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "macode"
dc.AutoIncrement = False
dc.Caption = "Material Code"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Material
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "mat"
dc.AutoIncrement = False
dc.Caption = "Material"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Finish
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "fin"
dc.AutoIncrement = False
dc.Caption = "Finish"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Designer
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "des"
dc.AutoIncrement = False
dc.Caption = "Designer"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Rendered Material / Property
dc = New DataColumn
dc.DataType = System.Type.GetType("System.String")
dc.ColumnName = "renmatp"
dc.AutoIncrement = False
dc.Caption = "Material Property"
dc.ReadOnly = False
dc.Unique = False
dt.Columns.Add(dc)

' Define Dataset
Dim ds As New DataSet
ds.Tables.Add(dt)

' Add Rows
Dim dr As DataRow
dr("id") = 0
dr("Item") = 0.ToString(PN, RPN, L, RSP, PDesc, MFGBY, REV, MFGCODE, MACODE, MAT, FIN, DES, RENMATP)
dt.Rows.Add(dr)

DataGrid1.DataSource = dt
DataGrid1.AllowSorting = True

'' Add Columns - simpler methodology worked great!
'dt.Columns.Add("Part Number", GetType(System.String))
'dt.Columns.Add("Reference Part Number", GetType(System.String))
'dt.Columns.Add("Length", GetType(System.Single))
'dt.Columns.Add("RSP", GetType(System.Boolean))
'dt.Columns.Add("Part Description", GetType(System.String))
'dt.Columns.Add("Manufactured By", GetType(System.String))
'dt.Columns.Add("Revision", GetType(System.String))
'dt.Columns.Add("Manufacturer Code", GetType(System.String))
'dt.Columns.Add("Material Code", GetType(System.String))
'dt.Columns.Add("Material Description", GetType(System.String))
'dt.Columns.Add("Part Finish", GetType(System.String))
'dt.Columns.Add("Designer", GetType(System.String))
'dt.Columns.Add("Material Property", GetType(System.String))

'' Add Rows
'dt.Rows.Add(New String() {PN, RPN, L, RSP, PDesc, MFGBY, REV, MFGCODE, MACODE, MAT, FIN, DES, RENMATP})

'' Update Data to DataGrid1
'DataGrid1.DataSource = dt
'DataGrid1.AllowSorting = True

End Sub
 
If you're grid isnt populating when applying a tablestyle, its usually because the mappingname is wrong. Also you have to add your tablestyle to the grid.
Code:
datagrid1.TableStyles.Add(TableStyle)

All you have to do to populate a grid at its simplest is to give it a datasource, which is usually (but not always) derived from a query from a database such as SQL and Access. I can see from your code that you are creating your own table schema, which is fine also.

You might want to take a look at thread796-1029632, which is a Datagrid class

Its possible to build a grid from any datasource, so long as you end up with the same datatable schema.









Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Okay, I've got this working, but I have no idea how to relate styles to it.

' Part No.
Dim aCol0 As New DataColumn
With aCol0
.DataType = System.Type.GetType("System.String")
.ColumnName = "PN"
.Caption = "Part No."
End With


In this code, the captions are using the Column Names. I cannot control the width. I would also like to use combo boxes for some of the columns. I am totally baffled as to how to do this. Note, these are unbound columns.


Thanks for your reply, and any additional help is greatly appreciated!


Public Sub PopulateDataGrid1()
' Define Column Data

' Part No.
Dim PN As String
If Len(oProp3(2).Value + "") > 0 Then
PN = oProp3(2).Value
Else
PN = ""
End If

' Reference Part No.
Dim RPN As String
If Len(oProp4(7).Value + "") > 0 Then
RPN = oProp4(7).Value
Else
RPN = ""
End If

' Length
Dim L As Single
If Len(oProp4(8).Value) > 0 Then
L = oProp4(8).Value
Else
L = 0
End If

' Recommended Spare Part
Dim RSP As Boolean
If Len(oProp4(6).Value) > 0 Then
RSP = oProp4(6).Value
Else
RSP = False
End If

' Part Description
Dim PDesc As String
If (Len(oProp3(14).Value + "") > 0) Or (Len(oProp3(3).Value + "") > 0) Then
PDesc = oProp3(14).Value & " " & oProp3(3).Value
Else
PDesc = ""
End If

' Manufactured By
Dim MFGBY As String
If Len(oProp3(15).Value + "") > 0 Then
MFGBY = oProp3(15).Value
Else
MFGBY = ""
End If

' Revision
Dim REV As String
If Len(oProp1(7).Value + "") > 0 Then
REV = oProp1(7).Value
Else
REV = ""
End If

' Manufacturer Code
Dim MATCODE As String
If Len(oProp4(5).Value + "") > 0 Then
MATCODE = oProp4(5).Value
Else
MATCODE = ""
End If

' Material Code
Dim MACODE As String
If Len(oProp4(2).Value + "") > 0 Then
MACODE = oProp4(2).Value
Else
MACODE = ""
End If

' Material Description
Dim MAT As String
If Len(oProp4(3).Value + "") > 0 Then
MAT = oProp4(3).Value
Else
MAT = ""
End If

' Part Finish
Dim FIN As String
If Len(oProp4(1).Value + "") > 0 Then
FIN = oProp4(1).Value
Else
FIN = ""
End If

' Designer
Dim DES As String
If Len(oProp1(3).Value + "") > 0 Then
DES = oProp1(3).Value
Else
DES = ""
End If

' Rendered Material / Property
Dim RENMATP As String
If Len(oProp3(10).Value + "") > 0 Then
RENMATP = oProp3(10).Value
Else
RENMATP = ""
End If

' Define Table
Dim aPartsTable As New DataTable

' Part No.
Dim aCol0 As New DataColumn
With aCol0
.DataType = System.Type.GetType("System.String")
.ColumnName = "PN"
.Caption = "Part No."
End With

' Reference Part No.
Dim aCol1 As New DataColumn
With aCol1
.DataType = System.Type.GetType("System.String")
.ColumnName = "RPN"
.Caption = "Reference Part Number"
End With

' Length
Dim aCol2 As New DataColumn
With aCol2
.DataType = System.Type.GetType("System.Single")
.ColumnName = "L"
.Caption = "Length"
End With

' Recommended Spare Part
Dim aCol3 As New DataColumn
With aCol3
.DataType = System.Type.GetType("System.Boolean")
.ColumnName = "RSP"
.Caption = "RSP"
End With

' Part Description
Dim aCol4 As New DataColumn
With aCol4
.DataType = System.Type.GetType("System.String")
.ColumnName = "PDesc"
.Caption = "Part Description"
End With

' Manufactured By
Dim aCol5 As New DataColumn
With aCol5
.DataType = System.Type.GetType("System.String")
.ColumnName = "MFGBY"
.Caption = "Manufactured By"
End With

' Revision
Dim aCol6 As New DataColumn
With aCol6
.DataType = System.Type.GetType("System.String")
.ColumnName = "REV"
.Caption = "Revision"
End With

' Material Code
Dim aCol7 As New DataColumn
With aCol7
.DataType = System.Type.GetType("System.String")
.ColumnName = "MATCODE"
.Caption = "Material Code"
End With

' Manufacturer Code
Dim aCol8 As New DataColumn
With aCol8
.DataType = System.Type.GetType("System.String")
.ColumnName = "MACODE"
.Caption = "Manufacturer Code"
End With

' Material Description
Dim aCol9 As New DataColumn
With aCol9
.DataType = System.Type.GetType("System.String")
.ColumnName = "MAT"
.Caption = "Material Description"
End With

' Part Finish
Dim aCol10 As New DataColumn
With aCol10
.DataType = System.Type.GetType("System.String")
.ColumnName = "FIN"
.Caption = "Part Finish"
End With

' Designer
Dim aCol11 As New DataColumn
With aCol11
.DataType = System.Type.GetType("System.String")
.ColumnName = "DES"
.Caption = "Designer"
End With

' Rendered Material / Property
Dim aCol12 As New DataColumn
With aCol12
.DataType = System.Type.GetType("System.String")
.ColumnName = "RENMATP"
.Caption = "Material Property"
End With

' Add Columns
With aPartsTable.Columns
.Add(aCol0)
.Add(aCol1)
.Add(aCol2)
.Add(aCol3)
.Add(aCol4)
.Add(aCol5)
.Add(aCol6)
.Add(aCol7)
.Add(aCol8)
.Add(aCol9)
.Add(aCol10)
.Add(aCol11)
.Add(aCol12)
End With

' Bind the Datatable to the Datagrid
DataGrid1.DataSource = aPartsTable

' Add Rows
Dim aRow As DataRow
aRow = aPartsTable.NewRow()

aRow("PN") = PN
aRow("RPN") = RPN
aRow("L") = L
aRow("RSP") = RSP
aRow("PDesc") = PDesc
aRow("MFGBY") = MFGBY
aRow("REV") = REV
aRow("MACODE") = MACODE
aRow("MATCODE") = MATCODE
aRow("MAT") = MAT
aRow("FIN") = FIN
aRow("DES") = DES
aRow("RENMATP") = RENMATP

aPartsTable.Rows.Add(aRow)

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top