none
How to get value of specific cell of selected row in datagridview

    Question

  •  Hi there , i want to get event when row selected ?

    which event handler method i have to choose 

    and i want to retrieve the cell value of any row selected with specific column index 2


    Hesham Hemdan Saleh

    Saturday, July 28, 2012 11:39 AM

Answers

  • What do you mean exacty? 

    When you click on RowHeader, or only on some cell, or ...?

    1. to get a specific (selected) cell without any event:

    string selectedCell = dgv[dgv.CurrentCell.ColumnIndex, CurrentCell.RowIndex].Value.ToString();

    2. to get a value of specfic column:

    selectedCell = dgv[2.ColumnIndex, CurrentCell.RowIndex].Value.ToString();

    3. to get the cell of a dgv` event:

    selectedCell = dgv[2, e.RowIndex].Value.ToString();

    4. for you to retreive some value of column index 2 of selected (or clicked) row I would suggest you to use CellClick event, which is we can say like a default event:

            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                string myValue = dataGridView1[2, e.RowIndex].Value.ToString();
                //instead of column index, you can specify the column name like: ["columnName", e.RowIndex]
            }


    Mitja

    • Marked as answer by HESHAM HEMDAN Saturday, July 28, 2012 1:10 PM
    Saturday, July 28, 2012 12:05 PM

All replies

  • What do you mean exacty? 

    When you click on RowHeader, or only on some cell, or ...?

    1. to get a specific (selected) cell without any event:

    string selectedCell = dgv[dgv.CurrentCell.ColumnIndex, CurrentCell.RowIndex].Value.ToString();

    2. to get a value of specfic column:

    selectedCell = dgv[2.ColumnIndex, CurrentCell.RowIndex].Value.ToString();

    3. to get the cell of a dgv` event:

    selectedCell = dgv[2, e.RowIndex].Value.ToString();

    4. for you to retreive some value of column index 2 of selected (or clicked) row I would suggest you to use CellClick event, which is we can say like a default event:

            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                string myValue = dataGridView1[2, e.RowIndex].Value.ToString();
                //instead of column index, you can specify the column name like: ["columnName", e.RowIndex]
            }


    Mitja

    • Marked as answer by HESHAM HEMDAN Saturday, July 28, 2012 1:10 PM
    Saturday, July 28, 2012 12:05 PM
  • i think the example of number 4 , is what i need , i will copy and check this code i hope its work , thank you and i will be back to you 

    Hesham Hemdan Saleh

    Saturday, July 28, 2012 12:49 PM