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

前端 未结 6 880
暖寄归人
暖寄归人 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: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.

提交回复
热议问题