DataGridView -Value does not gets saved if selection is not lost from a cell

前端 未结 6 845
暖寄归人
暖寄归人 2020-12-16 03:53

I am using the DataGridView Control for reading and writing an XML file through XML Serialization.

I have an issue as explained below:

  1. I read an XML fi
相关标签:
6条回答
  • 2020-12-16 04:07

    The best way (though quick and dirty) is to assign the currentCell value to Nothing.

    For example, in the save method, do:

    dgvMyGrid.CurrentCell = Nothing
    

    and then proceed further.

    0 讨论(0)
  • 2020-12-16 04:20

    You can get the value of a cell which is not yet committed using the EditedFormattedValue property for the current cell, as below

    dataGridView1.CurrentCell.EditedFormattedValue
    
    0 讨论(0)
  • 2020-12-16 04:21

    It's because the edited cell value is not committed to the DataSource until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler :

    void dataGridView1_CurrentCellDirtyStateChanged(object sender,
        EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 04:21

    If I understand you correctly, a cell is in editing mode and you're trying to programmatically stop editing and pass the value to the underlying datasource?

    I'm using a somewhat "dirty" approach to do that in one of my applications:

    if (dataGridView1.CurrentCell.IsInEditMode)    
    {    
        int y = dataGridView1.CurrentCellAddress.Y;    
        int x = dataGridView1.CurrentCellAddress.X;    
        if (y > 0)      
            dataGridView1.CurrentCell = dataGridView1.Rows[y - 1].Cells[x];    
        else    
            dataGridView1.CurrentCell = dataGridView1.Rows[y + 1].Cells[x];    
        dataGridView1.CurrentCell = dataGridView1.Rows[y].Cells[x];    
    }
    

    That piece of code first checks whether the current cell is in edit mode. Then it changes the current cell programmatically (either to the previous row or the next row in case we're in the first row). After that, it restores the current cell selection.

    You would call this code in your "File Save As" handler.

    0 讨论(0)
  • 2020-12-16 04:24

    I had the same situation and I was even using accelerator keys for save button for saving the grid values. When I click on Save button focus lost from DGV and hence cell value is committed, but when I use accelerator keys focus doesn't lost from DGV hence no committing of the cell value.

    After looking at the Amit Karmakar answer out of curiosity I tried that answer and it worked. To find out more details I went into debugging of the DGV and found that it is really same thing as commitedit which somehow doesn't work if you use it in the save button click.

    When we set CurrentCell of DGV to null, before setting it to null DGV first gets the edited value and pushes it in to cell value and then sets CurrentCell REFERENCE to null. Here it doesn't mean that it is setting underlying DGV cell to null. Hence this works perfectly for the above problem.

    Note: This solution may not work perfectly when you have validating events for the cell and if user enters invalid data which will fail validation. In this case setting current cell to null also fails as it cannot push the value to cell.

    I gave this explanation as I've raised question on Amit Karmakar answer asking how can it be possible. I thought it may help some other, so dropped this explanation as answer.

    0 讨论(0)
  • 2020-12-16 04:24

    OK, this is UGLY but it works to get the FINAL CHANGES from the grid WITHOUT having to move to another row:

    With DataGridView1
        .DataSource = Nothing
        .DataSource = gridDataTable
        Dim changedFoo As DataTable = gridDataTable.GetChanges
    End With
    

    However I still like the answer from Amit Karmakar the best. I've added the 'DataGridView1.CurrentCell = Nothing' to the DataGridView1 LostFocus event.

    0 讨论(0)
提交回复
热议问题