Programmatic Entry on databound datagridview's uncommitted row isn't causing new row to appear. When selected, entry disappears

StackOverflow https://stackoverflow.com/questions/14513893

  •  17-01-2022
  •  | 
  •  

Pergunta

I have a training project at my new job and a really small but crucial setback.

I have a datagridview control bound to a dataset read from an xml file. It has 3 columns: item, price, calories. I am supposed to calculate the average values for price and calories and place them in the row below the entries that are read in from the xml. This works fine, except that when I fill the bottom row with the averages, the row remains uncommitted and when a user selects the row, the values disappear. If you manually type an entry into the row, it commits as expected and a new uncommitted row appears. How do I get the same behavior from calculated entries placed programmatically on the bottom row? Since the dgview is databound I can't do dgview.rows.add or anything like that. My goes basically goes like this:

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

This puts the average values on the uncommitted row, which is the only available unfilled row. I would think that would cause a new row to appear, but it doesn't. The average row remains uncommitted and as I mentioned all data on the row disappears when the row is selected. How can I force the row to commit and show a new uncommitted row below it? Just as some additional information, I cannot use the microsoft.visualbasic library. Any assistance is appreciated. Thanks in advance.

Foi útil?

Solução

Right, the reason you databind something is because you want changes to that item to appear in the design view and/or to be able to manually add data to the source.

So instead of adding stuff programmatically to the item that display the dataset, you add the data to the dataset which then displays it.

ds.tables(0).Rows.Add()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top