How to send a string from a DataGridView Cell in a open form, to a Textbox in another form in a background.

  • Thread starter Thread starter F.oliveirarocha
  • Start date Start date
F

F.oliveirarocha

Guest
Hi folks,

I'm a knew user in a C#, I'm having some headache when making a consult in a form, and I need to transfer the content of that cell to a textbox of the Owner.

Simpifying: I click in a button to open a form with a DataGridView with Supplyer Code and Name. I select the desired Name, and want to send to a textbox (code and name) at the main form which is open in a background. got that?

Here is the code tha I'm using:

private void dataGridView_telaFornecedores_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
((CadastroTitulosaPagar)Owner).textBox_Fornecedor.Text = (string)((DataGridViewTextBoxCell)((DataGridView)sender).Rows[e.RowIndex].Cells[1]).Value;
((CadastroTitulosaPagar)Owner).Cod_FornecedorTextbox.Text = dataGridView_telaFornecedores.Rows[e.RowIndex].Cells["codFornecedor"].Value.ToString();
Close();
}
}

I got an ERROR message when I click to the selection: I'm making a translation on bold text.

"System.NullReferenceException: 'Reference of an object not defined for an instance of an object.'"

Can someone help me out bilding this transfer? I need to pass the two values code (int) and Name (string) in a DataGridView to the main form in each textbox.

I have this type of consult four times, defining: type of expense, Cost Center, Invoice Status and Supplier.

Thanks for your support.

Below is the entire Form code, is very simple.

namespace SGFRenaissance
{
public partial class TelaFornecedores : Form
{
public TelaFornecedores()
{
InitializeComponent();
}

private void TelaFornecedores_Load(object sender, EventArgs e)
{
Conexao_Fornecedor();
}

private void Conexao_Fornecedor()
{
this.fornecedorContasaPagarBindingSource.DataSource = DataContextFactory.DataContext.Fornecedor_Contas_a_Pagars;
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void dataGridView_telaFornecedores_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
((CadastroTitulosaPagar)Owner).textBox_Fornecedor.Text = (string)((DataGridViewTextBoxCell)((DataGridView)sender).Rows[e.RowIndex].Cells[1]).Value;
((CadastroTitulosaPagar)Owner).Cod_FornecedorTextbox.Text = dataGridView_telaFornecedores.Rows[e.RowIndex].Cells["codFornecedor"].Value.ToString();
Close();
}
}


}
}

Continue reading...
 
Back
Top