Introduction
This tip will help developers to understand how to retrieve current exact value from datagridview cell.
DataGridViewCell.Value Property
"DataGridViewCell.Value
" or "Value
" 's function is to get or set the value of the associated cell but there is a problem with the Property "Value
" viz., every time you leave the cell, formatting is needed to display the content on the user interface. While you use "Value
", sometimes it does not format the content of the cell and when you try to retrieve the data like this:
string str = datagridview1.Rows[0].Cells[0].Value.ToString();
Output
You might get some previously written data or null value.
DataGridViewCell.EditedFormattedValue Property
"DataGridViewCell.EditedFormattedValue
" Or "EditedFormattedValue
" 's function is to get the current, formatted value of the cell, regardless of whether the cell is in the edit mode and the value has not been committed. EditedFormattedValue
ensures that the value is committed or saved at every point of coding. You can use the property like this:
string str = dataGridView1.Rows[0].Cells[0].EditedFormattedValue.ToString();
Output
This will give you the current exact value of the dataGridView's cell.