1

在我的新工作中,我有一个培训项目,并且遇到了一个非常小的但至关重要的挫折。

我有一个 datagridview 控件绑定到从 xml 文件读取的数据集。它有 3 列:项目、价格、卡路里。我应该计算价格和卡路里的平均值,并将它们放在从 xml 读取的条目下方的行中。这很好用,除了当我用平均值填充底行时,该行保持未提交状态,当用户选择该行时,值消失。如果您手动在该行中键入一个条目,它会按预期提交并出现一个新的未提交行。如何从以编程方式放置在底行的计算条目中获得相同的行为?由于 dgview 是数据绑定的,我不能做 dgview.rows.add 或类似的事情。我的基本上是这样的:

Private Sub PopulateDGView(ByVal sFilePath As String)
Dim dgview As New DataGridView
Dim ds As New dataset
Dim dPriceAvg As Double = 0
Dim dCaloriesAvg As Double = 0
ds.readxml(sFilePath)

dgview.datasource = ds
dgview.DataMember = "food"

'[Here I loop through the rows, sum the values and divide by the number of rows to get
 'the average values for the columns then write the following]

dgview.Rows(dgview.Rows.GetLastRow(DataGridViewElementStates.Displayed)). _
Cells("Item").Value = "Average"
dgview.Rows(dgview.Rows.GetLastRow(DataGridViewElementStates.Displayed)). _
Cells("Price").Value = dPriceAvg.ToString
dgview.Rows(dgview.Rows.GetLastRow(DataGridViewElementStates.Displayed)). _
Cells("Calories").Value = dCaloriesAvg.ToString
End Sub

这会将平均值放在未提交的行上,这是唯一可用的未填充行。我认为这会导致出现新行,但事实并非如此。当我提到的行选择行时,平均行仍然不承诺,并且当我提到该行上的所有数据都消失了。如何强制该行提交并在其下方显示一个新的未提交行?正如一些附加信息一样,我不能使用 microsoft.visualbasic 库。任何帮助表示赞赏。提前致谢。

4

1 回答 1

0

是的,您对某些内容进行数据绑定的原因是因为您希望对该项目的更改出现在设计视图中和/或能够手动将数据添加到源中。

因此,不是以编程方式将内容添加到显示数据集的项目中,而是将数据添加到数据集,然后显示它。

ds.tables(0).Rows.Add()
于 2013-01-25T09:21:35.290 回答