Pass an element of a Datagrid

0

Hello good day I have an existential question, I am filling a DataGrid by means of a query in C# windows forms , Which shows me in a column several ID , and by middle of the function CellDoubleClick in the DataGrid I am showing a window in which I intend to show in a Textbox the number of ID that I select from the DataGrid only that I do not remember how to pass that value as a parámetro to the new form where is the textbox , someone can help me, I'm stopped at this time. I leave the code of how I'm doing it.

Form1

       private void dgvInfoNomina_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                frmTimbraNomina objTimbrar = new frmTimbraNomina();
                objTimbrar.ShowDialog();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }
        public string EnviarId(string nomina)
        {           
            string IdNomina = "";
            IdNomina = dgvInfoNomina.CurrentCell.Value.ToString();
            return IdNomina;
        }

Form2

 public void MostrarId()
    {
        frmMuestraNomina objMostrar = new frmMuestraNomina();
        objMostrar.EnviarId()
    }
    
asked by Alberto Arenas 09.04.2017 в 19:27
source

2 answers

1

There are many ways to do this. I'll leave an option in steps:

  • Crear una variable Publica in the Form2 of the desired type, in this case String

    public String valor;

  • In the Load of Form2 assign the valor of the variable to the TextBox desired.

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = valor;
    }
    
  • In the Form1 in the Double_Click , create e Instanciar the Form2 , then through that instance access the variable value that was created and assign the value of the selected cell.

    private void dgvInfoNomina_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
    try
      {
        Form2 frm= new Form2();
        frm.valor = dgvInfoNomina.CurrentCell.Value.ToString();
        frm.ShowDialog();
      }
    catch (Exception ex)
      {
            ex.ToString();
      }
    
  • answered by 09.04.2017 / 21:32
    source
    1

    I guess you want something like this:

    private void dgvInfoNomina_CellDoubleClick(object sender, DataGridViewCellEventArgs e){
    int col_ID = 0; //suponiendo que el id se encuentre en la columna 0
    string id = (string)dgvInfoNomina[e.RowIndex, col_ID].Value;
    //Llamas a tu form y le pasas el id como parametro
    }
    
        
    answered by 09.04.2017 в 21:27