我从数据库返回一组结果行,我想在UI上为HTML控件分配这些值。
我从DB中将数据提取到DataSet中。
对于ex,我的数据如下所示:
ID EmpId Question Comments
1 2 abcdefgh comments1
2 6 xyhgjkjh comments2
3 6 kjhkjhjk comments3
4 6 uyyiuyuui comments4
5 3 erteyeyuy comments5
6 6 qapooioip comments6
基于输入(EmpId),我获取数据。对于ex EmpId = 6,我想使用HTML字段在UI上显示上述数据,如下所示:
Q1。 xyhgjkjh
comments2
Q2。 kjhkjhjk
comments3
Q3.uyyiuyuui
comments4
Q4.qapooioip
comments6
我只想通过循环数据库中的结果集来显示HTML标签。
我正在创建以下属性:
Public class MyClass
{
public int Id{get;set;}
public int EmpId{get;set;}
public String Question{get;set;}
public String Comments{get;set;}
}
我想使用上面的类并将结果集值设置为属性并循环遍历它并分配给UI上的HTML控件。
如何转换数据集并将其设置为MyClass obj属性并将其分配给VB.NET中的HTML控件。
我是否需要具有List for Question属性?
有任何帮助吗?提前谢谢。
答案 0 :(得分:1)
使用你的班级:
' I'm going to assume that the class properties are in the same order as the dataset and that there is only one table in the dataset.
Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyClassList as List(Of TheClassInstance)
Dim MyDataRow as DataRow
For Each MyDataRow in TheDataSet.Tables(0).Rows
TheClassInstance = New MyClass
TheClassInstance.Id = MyDataRow(0)
TheClassInstance.EmpId = MyDataRow(1)
TheClassInstance.Question = MyDataRow(2)
TheClassInstance.Comments = MyDataRow(3)
MyClassList.Add(TheClassInstance)
' Everytime you loop you're adding an instance of the class to the list
Next
要访问该类的每个实例,您可以使用for循环或循环使用变量作为索引来循环,以用于获取列表中的数据。为每个循环使用a:
Dim MyClassInstance as new MyClass
For Each MyClassInstance in MyClassList
' You can set the text property of labels and textbox controls to the data
Textbox_Comments.text = MyClassInstance.Comments
' ... etc
Next
编辑为每个父母提出多项问题:
改变你的班级:
Public class MyClass
Public Property Id as Integer
Public Property EmpId as Integer
Public Property Question as List(of String)
Public Property Comments as String
End Class
我假设有一个包含EmplId的父表和每个EmplID的子表问题:
Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyDataRow as DataRow
Dim MyDataRow2 as DataRow ' For clarity
For Each MyDataRow IN ParentDataSet.Tables(0).Rows
TheClassInstance = New MyClass
TheClassInstance.Id = MyDataRow(0)
TheClassInstance.EmpId = MyDataRow(1)
TheClassInstance.Comments = MyDataRow(3)
Dim MySecondDataSet as Dataset = GetQuestions(MyDataRow(1) ' Get questions for EmplId
For Each MyDataRow2 in MySecondDataSet.Tables(0).Rows
' It's assumed that the question is the only column in the table
TheClassInstance.Questions.Add(MyDataRow2(0))
Next
Next
当所有问题都在表格的一行中时:
Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyDataRow as DataRow
For Each MyDataRow IN ParentDataSet.Tables(0).Rows
TheClassInstance = New MyClass
TheClassInstance.Id = MyDataRow(0)
TheClassInstance.EmpId = MyDataRow(1)
TheClassInstance.Comments = MyDataRow(3)
' I assume that the questions start in column 4
' Add each question in the row, I assume 4 questions in order
TheClassInstance.Questions.Add(MyDataRow(4))
TheClassInstance.Questions.Add(MyDataRow(5))
TheClassInstance.Questions.Add(MyDataRow(6))
TheClassInstance.Questions.Add(MyDataRow(7))
Next
答案 1 :(得分:0)
另一种创建将DataRow
转换为班级MyClass
Public Function ToMyClass(row As DataRow)
Return New MyClass With
{
.Id = row.Field(Of Integer)("ID"),
.EmpId = row.Field(Of Integer)("EmpID"),
.Question = row.Field(Of String)("Question"),
.Comments = row.Field(Of String)("Comments"),
}
End Function
通用方法DataRow.Field(Of T)
会将列值转换为期望的类型,这会提供编译器提供的某种类型安全性 - 您需要将Option Strict
设置为On
。
然后,您将能够在"一行"
中创建课程列表Dim data As DataTable = dataset.Tables(0)
Dim questions As List(Of MyClass) =
data.AsEnumerable().Select(AddressOf ToMyClass).ToList()