【问题标题】:read and modify/update cell in datatable读取和修改/更新数据表中的单元格
【发布时间】:2013-12-15 21:30:03
【问题描述】:

场景
我想通过比较种族、宗教、性别、口语等参数来计算和排名医护人员对特定患者的适用性。

HealthcareWorker table
----------------------
ID (int)
name (varchar)
race (varchar)
religion (varchar)
gender (varchar)
german (bit)
french (bit)
english (bit)


Patient table
-------------
ID (int)
name (varchar)
race (varchar)
religion (varchar)
gender (varchar)
german (bit)
french (bit)
english (bit)


网页布局
page1.aspx 上将有一个gridview 表显示所有Patients 的详细信息。 然后用户将选择Patient 并被定向到page2.aspx
page2.aspx 上将有一个表格显示每个Healthcare Worker 的详细信息以及他与所选patient 的适合性,以score 的形式出现。表格内还将有checkboxes,以便将多个Healthcare Worker 分配给选定的Patient

page2.aspx
| ID |  name | religion | gender  |  german  |  french |  english |  score |          |
|----+-------+----------+---------+----------+---------+----------+--------+----------|
| 4  |  mary |          |    f    |    1     |    0    |    1     |   132  | checkbox |
| 2  |  john |          |    m    |    0     |    1    |    1     |   125  | checkbox |
| 3  |  tim  |          |    m    |    1     |    0    |    1     |    98  | checkbox |
| 1  |  jane |          |    f    |    1     |    1    |    0     |    55  | checkbox |


分数的计算是这样的:

if patient.race = healthcareworker.race then healthcarework.score +10
if patient.religion = healthcareworker.religion then healthcarework.score +10
if at least one of patient's language match with healthcareworker's language then healthcarework.score +10


代码:

    Dim strCon As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
    Dim con As New SqlConnection(strCon)

    Dim query As String = "select *, CAST ( 0 as int ) score from HealthcareWorker;"

    Dim cmd As SqlCommand = New SqlCommand(query, con)

    con.Open()

    Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    Dim dt As DataTable = New DataTable()
    dt.Load(dr)

    'code to read cell
    'code to compare and compute
    'code to update the score in each row

    GridView1.DataSource = dt
    GridView1.DataBind()

    con.Close()

如何读取和修改数据表中的单元格? 我想读取并比较每行中某些单元格的数据,然后更新score 值。

我需要为每一行更新score,所以我需要一个循环,但我不知道事先将加载到datatable 中的行数。 如何获取datatable 中的行数?



编辑/更新:修改了整个问题以提供(很多)更多背景信息

【问题讨论】:

  • 为什么不在数据库里做呢?所以用实际计算替换CAST ( 0 as int )。除此之外,根本不清楚您要如何计算分数,select * 没有帮助。
  • 我正在简化我的实际场景以使其更易于解释。不要担心我如何计算scorescore 更多的是决策配对矩阵得分而不是游戏得分。 score 不存储在数据库中,而是即时计算和存储,因为 score 仅对 table1 中某一行中的值与其他表中另一行中的值的组合有效.
  • 这并不能解释为什么你不能在sql中计算它。您可以在 table.Rows 上使用 for 循环或 foreach 循环。但是因为我不知道架构,所以我不能举一个例子。要访问和转换列,请使用 row.Field。
  • 重做整个问题

标签: asp.net vb.net webforms datatable


【解决方案1】:

最后一个问题很容易回答:

我需要更新每一行的分数,所以我需要一个循环 但我不知道将加载到 手头的数据表。 如何获取 数据表

您只需要使用Rows.Count 属性:

Dim rowCount As Int32 = tblPatient.Rows.Count ' tblPatient => DataTable '

如果你想循环所有行,你可以使用foreach:

For Each row As DataRow In tblPatient.Rows
    ' ... '
Next

for-loop:

For i As Int32 = 0 To tblPatient.Rows.Count - 1
    Dim row As DataRow = tblPatient.Rows(i)
    ' ... '
Next

要更新行,您可以使用SetField 扩展方法,它也支持可空类型:

For Each row As DataRow In tblPatient.Rows
    Dim id As Int32 = row.Field(Of Int32)("ID")
    Dim race As String = row.Field(Of String)("Race")
    row.SetField("Score", CalculateScore(row))
Next

抱歉,我无法为您提供正确的计算方法,但由于您尚未提供Score 的来源(没有列),因此仍不清楚您要如何计算它。但无论如何它可能会给你一个想法。

【讨论】:

  • 谢谢,现在我知道该怎么做了。为了其他可能正在查看此内容的人,For Each 循环中的SetField 不起作用,除非我在loop 之前添加tblPatient.Columns("Score").ReadOnly = False。以某种方式在 .aspx 中设置 readonly = false 不起作用,它只有在 .aspx.vb 中完成时才有效
猜你喜欢
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2022-08-06
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多