0

When I save a new item on datagridview the statement

MessageBox.Show(this.tb_aprovacao_admissaoDataGridView.CurrentRow.Cells[0].Value.ToString());

shows the value -1. How can I change it to show the real number of ID?

Thanks to everyone.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    -1 means it isn't found, so something wrong with the search itself, can you show some more code, might be because you request something to early or so. – Random IT Guy Feb 02 '13 at 12:25
  • Are you trying to save an item, then show that item, or are you trying to just save the item/ show the item? – Random IT Guy Feb 02 '13 at 12:48
  • the datagridveiw show -1 on ID when I am editing a new item. And I need the ID number to include on foreign key of other table. – Mauricio Feb 04 '13 at 14:51

1 Answers1

0

Depends how you want to get the value.. Do you want to get the value after you click on the cell, or on the click of a button or?

If you want to do it on the cell click event, you can do it like this:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
            {
                 MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            }
        }

To get it with a button:

private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
    }
Random IT Guy
  • 625
  • 1
  • 8
  • 16
  • When I add a new item the first number displayed on ID is -1. And when update the datagridview this item show its ID with real value (Ex. 7). I need this real number of ID for include on foreign key on other table after save this new item. – Mauricio Feb 04 '13 at 14:38