how to get cell value in datagridview c# code example

Example 1: datagridview cellvaluechanged get value

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    newvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    oldvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

Example 2: datagridview select row column cell c#

private void setCurrentCellButton_Click(object sender, System.EventArgs e)
{
    // Set the current cell to the cell in column 1, Row 0. 
    this.dataGridView1.CurrentCell = this.dataGridView1[1,0];
}