1

I have WinForm application and I like to get row values of edited cell like below example. After click on georce cell to change the text as "Matar", then clickbutton1. Get the row values of "george" like below with "Matar" value. Inside my code I only get the value of edited cell.

GridView
--------
ID   Name   School_ID
---  ----   ---------
0    adam   300654     button1
1    steve  300789
2    george 455554
.    .       .
.    .       .
.    .       .
10   billy  562211

result:

ID:2   Name:Matar   School_ID: 455554

mycode:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show(dataGridView1[e.ColumnIndex, e.RowIndex]
              .Value
              .ToString());
}

2 Answers 2

1

If you would like to get value on value change then you can get value like this.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    string sValue = string.Empty;
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
        sValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

}

To get value from Current Row

string sValue = string.Empty;
if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.Cells[e.ColumnIndex].Value != null)
    sValue = dataGridView1.CurrentRow.Cells[e.ColumnIndex].Value.ToString();

To get value from Current Cell

string sValue = string.Empty;
if (dataGridView1.CurrentCell != null && dataGridView1.CurrentCell.Value != null)
    sValue = dataGridView1.CurrentCell.Value.ToString();

To get value from Specific Row-Cell

string sValue = string.Empty;
if (dataGridView1.Rows[RowIndex].Cells[ColumnIndex].Value != null)
    sValue = dataGridView1.Rows[RowIndex].Cells[ColumnIndex].Value.ToString();

EDITED:

To get full row cell value

StringBuilder sb = new StringBuilder();
int RowIndex = 0; //set the row index here.
for (int iCol = 0; iCol < dataGridView1.ColumnCount; iCol++)
{
    if (dataGridView1.Rows[RowIndex].Cells[iCol].Value != null)
        sb.Append(dataGridView1.Columns[iCol].HeaderText + ":" + dataGridView1.Rows[RowIndex].Cells[iCol].Value.ToString() + ",");
    else
        sb.Append(dataGridView1.Columns[iCol].HeaderText + ":,");
}
string RowValues = sb.ToString();
//To Remove the last seperator ","
RowValues = RowValues.Substring(0, RowValues.Length - 1);

it is necessary to check is value not null otherwise it will give exception "Reference Object not set."

6
  • I want to get the whole row value, not a single cell value like my result. like ID:2 Name: Matar School_ID: 455554. Mar 21, 2014 at 4:06
  • there is no functionality to get that directly. you have to create a loop and concate all value in a string variable.
    – Shell
    Mar 21, 2014 at 4:14
  • should I put this inside dataGridView1_CellValueChanged event? Mar 21, 2014 at 4:23
  • yes you can place this code in the cellvaluechange event. but, if you want to get that value after assigning value in full row then i would prefer to place this code in RowValidating.
    – Shell
    Mar 21, 2014 at 4:25
  • it is giving me only first row values, but I want the row that I change the cell values. I am changing the third column of the seventh row. but still getting first row values Mar 21, 2014 at 4:32
0

To get the whole row, you could access the Rows collection:

dataGridView1.Rows[e.RowIndex]

var firstColumn = Convert.ToString(dataGridView1.Rows[0].Cells[0].Value);

You could also try the CurrentRow property:

dataGridView1.CurrentRow

var firstColumn = Convert.ToString(dataGridView1.CurrentRow.Cells[0].Value);
5
  • this not giving me the whole rows value... it gives me only single cell value Mar 21, 2014 at 4:08
  • 1
    The first line of code in each block shows you how to access the entire row. But if you want to display the contents of the row, you'll still have to access the cells individually. The second line of code in each block is how you access an individual cell in each case. Mar 21, 2014 at 4:10
  • I am confused... How can I see the result on screen maybe on messageBox like this: ID:2 Name:Matar School_ID: 455554 Mar 21, 2014 at 4:18
  • 1
    Ditto on what Nimesh said. Loop through all the cells in your row and concatenate them or add them to a StringBuilder. Mar 21, 2014 at 4:20
  • I am now trying Nimesh solution. Mar 21, 2014 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.