Play value of a grid that is in form to another grid in another form C # windows form

2

Hello, I'm here with one more situation requesting help because I have not yet found it in the searches.  I have a form that I put a Gridview in the form has button to open another form of query and when I click on an item from the list it adds in the form that is already open with the empty grid still follows code that I am trying to use: This is the initial form where the

  public partial class frmPedidos : Form
{

    public frmPedidos()
    {
        InitializeComponent();
    }

    ConexaoClienteDataContext cc = new ConexaoClienteDataContext();

    private void btnProcSabor_Click(object sender, EventArgs e)
    {


        frmSlaveConsultaProduto _frmPRoduto = new frmSlaveConsultaProduto();
        if (_frmPRoduto.ShowDialog() != DialogResult.OK)
            return;
        dgvItemPedido.CurrentRow.Cells[1].Value = _frmPRoduto.Codigo;
        dgvItemPedido.CurrentRow.Cells[3].Value = _frmPRoduto.Valor;
    }
}

This would be a query form and will allow you to click and send to the other grid of the main form

 public partial class frmSlaveConsultaProduto : Form
{
        public int Codigo { get;  set; }
        public string Nome { get;  set; }
        public double Valor { get;  set; }
        public int quantidade { get;  set; }

    public frmSlaveConsultaProduto()
    {
        InitializeComponent();
    }
    ConexaoClienteDataContext cc = new ConexaoClienteDataContext();
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {



      Codigo=Convert.ToInt32( dgvPesquisaProd.CurrentRow.Cells[0].Value.ToString());
       Valor = Convert.ToDouble(dgvPesquisaProd.CurrentRow.Cells[2].Value.ToString());
        this.DialogResult = DialogResult.OK;

    }

The way it is it returns an error:

Additional information: Object reference not set to an instance of an object.

Thanks in advance for the help.

    
asked by anonymous 02.11.2017 / 01:23

1 answer

1

I recommend changing the event CellContentClick to CellMouseDoubleClick so that the user gives two clicks to select the desired record.

Referral Error may be happening because Cell's .Value can be null, and would use the Row Index that was clicked. Here is sample code:

    public int Codigo { get; set; }

    private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex >=0) //verifica se não clicou no header da coluna 
        {
            //Considerando que a Cell 0 nunca será nulo, não fiz a validação
            this.Codigo = (int)((DataGridView)sender).Rows[e.RowIndex].Cells[0].Value;
        }
    }

In the Order Form part:

private void btnProcSabor_Click(object sender, EventArgs e)
{
    frmSlaveConsultaProduto _frmPRoduto = new frmSlaveConsultaProduto();
    if (_frmPRoduto.ShowDialog() == DialogResult.OK)
    {
        //Certeza que aqui você tem uma CurrentRow ? 

        dgvItemPedido.CurrentRow.Cells[1].Value = _frmPRoduto.Codigo;
    }
}
    
02.11.2017 / 02:47