Tour DataGridView in C #

0

Good day requesting your support to go through DataGridView in C # and get the value of each ID in a column ie if I have a number N < /> ID's within DataGrid , by double clicking on each one of them send me a MessageBox in which you indicate the number of ID that I selected.

Try to do it with the following code but I only got the value of the last ID

for (int i = 0; i < dgvInfoNomina.Rows.Count; i++)
{
  dgc = dgvInfoNomina.Rows[i].Cells[0];
  IdNomina = (dgc.Value.ToString());
}
MessageBox.Show("El Id es" + IdNomina);
    
asked by Alberto Arenas 07.04.2017 в 19:31
source

2 answers

0

I found it, it's as simple as the following lines:

string cadena="";

cadena=datagridview1.CurrentCell.Value.ToString();

MessageBox.Show(cadena);

It is necessary to clarify that in the multiple event selection we must leave the value in false.

    
answered by 07.04.2017 / 20:59
source
3

If your gridview only allows you to select one row at a time you do not need to iterate over the row collection, just use:

var IdNomina = dgvInfoNomina.SelectedRows[0].Cells[0].ToString();

To access the selected id, if you have multiselection, you must iterate over the collection of selected rows:

for (int i = 0; i < dgvInfoNomina.SelectedRows.Count; i++)
            {
                var IdNomina = dgvInfoNomina.SelectedRows[i].Cells[0].ToString();
                MessageBox.Show("El Id es" + IdNomina);
            }

In this case keep the MessageBox.Show within the cycle for or just show you the last id accessed by the cycle.

    
answered by 07.04.2017 в 19:46