DataRow.Delete () command by row index not known

1

To remove Row from a dataGridView using DataTable I can use the DataRow.Delete() command but for example, in the code below it excludes a Row already known, and in this case I need to remove the% Row that I'm selecting with the mouse click on dataGridView .

I thought I might remove it by index , because each line created has a index (as far as I understand), so if I can get the index from the selection I can delete the line from DataTable e of dataGridView .

If someone can help me with how to get the index of the selected line, I appreciate it.

     <//Código exemplo
     for (int i = dt.Rows.Count - 1; i >= 0; i--)
    {
        DataRow dr = dt.Rows[i];
        if (dr["name"] == "Joe")
            dr.Delete();
    }>

    DataTable dt = new DataTable("Cadastro");

     <
     //botão para salvar o que eu digitei nas text box (informa o nome e o email do usuario na textbox e mostra no dataGridView)
     private void bt_salvar_Click_1(object sender, EventArgs e)
{


    Pessoa dados = new Pessoa(txt_nome.Text, txt_email.Text);
    DataRow dr = dt.NewRow();

    dr["Nome"] = dados.Nome;
    dr["Email"] = dados.Email;


    dt.Rows.Add(dr);
    dataGridView1.DataSource = dt;


}>

Note: I am not using a database and the application is in WinForms.

    
asked by anonymous 08.03.2017 / 18:34

1 answer

1

The DataGridView has the property CurrentCell that has the property RowIndex , which returns the index of the row of the cell that is active / selected.

To use just do the following:

int indexDaLinhaSelecionada = datagridview1.CurrentCell.RowIndex;
    
08.03.2017 / 18:39