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

Asked

Viewed 432 times

2

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

  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 the query form and will allow clicking and sending 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: Undefined object reference for an object instance.

Thanks for your help.

inserir a descrição da imagem aqui

1 answer

1


Recommend changing the event CellContentClick for CellMouseDoubleClick for the user to make two clicks to select the desired record.

The Reference Error may be happening because Cell’s . Value may be null, and would use the Row Index that was clicked. Follow the example 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;
    }
}
  • So I put a picture there so have with your example most apparently is not bringing any value. Is it because I’m not putting the rest of the grid values ? even though I have set the fields not mandatory?

  • put a pointbreak on the line, and check at runtime if it has any value

  • +this{Adsintegrados.View.frmSlaveConsultProduct, Text: frmSlaveConsultProduct} Adsintegrados.View.frmSlaveConsultProduct this.Code 7 int this.Value 11 double Apparently in the selection form took the value now in the form that will receive it gets even more from the error of references -Object reference not defined for an instance of an object.

  • some object you are trying to use is null, just see which is

  • does it have something to see with the bank ? because in the grid that receives it is only to list not done anything to insert in it. My thought was to add in the grid and save after ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.