4
I have a form called TelaInicio
where I have a method CarregarGrid()
I would like to access this method through my other form Cadastrar
I’ve left the method CarregarGrid()
as public but still do not have access to it I instated the form TelaInicio
thus
public Cadastrar(TelaInicio telainicio)
{
InitializeComponent();
this.telainicio = telainicio;
}
and in the TelaInicio
I call him on a button
private void Cadastrar_Click(object sender, EventArgs e)
{
Cadastrar form = new Cadastrar(this);
form.ShowDialog();
}
and in the TelaInicio
my method is like public void CarregaGrid()
, but still in the other form when I try to access it says that the method does not exist
public void CarregarGrid()
{
try{
//indico o número de colunas
dgvDados.ColumnCount = 14;
objConnection = new MySqlConnection(caminho);
//instância do comando onde passo
//o sql e a conexão como parâmetro
objComando = new MySqlCommand("SELECT * FROM checagens", objConnection);
//abro a conexão
objConnection.Open();
//instâncio o leitor
var leitor = objComando.ExecuteReader();
//enquanto leitor está lendo
while (leitor.Read())
{
//insiro os dados no dgvDados
dgvDados.Rows.Add(leitor[0].ToString(),
leitor[1].ToString(),
leitor[2].ToString(),
leitor[3].ToString(),
leitor[4].ToString(),
leitor[5].ToString(),
leitor[6].ToString(),
leitor[7].ToString(),
leitor[8].ToString(),
leitor[9].ToString(),
leitor[10].ToString(),
leitor[11].ToString(),
leitor[12].ToString(),
leitor[13].ToString());
}
}
catch(Exception ex){
MetroFramework.MetroMessageBox.Show(this, "" + ex, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally{
//fecho conexão
objConnection.Close();
}
}
I’m calling the method inside the button
Register in the form Cadastrar
if (cadchecagem(checagem))
{
MetroFramework.MetroMessageBox.Show(this, "Dados cadastrados com sucesso ", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Utilidades.Funcoes.LimparCampos(gbchecagem);
CarregarGrid();
}
}
It presents the error:
The name 'Carregargrid' does not exist in the Current context
Where is the "Carregargrid" method? Preferably put the 2 classes in your question to facilitate analysis
– Victor Laio
I edited the question and added the method but I believe it will not make a difference you see it
– Patrick Perdigão
I did not locate where you are trying to access the method of one form through another, you need to include the code snippet where you are presenting the error.
– Leandro Angelo
You can put it as static or better create a manager to access the entire class.
– brunox99x
Hello Patrick. Put the code part where you call the method
CarregarGrid()
from theCadastrar
.– CypherPotato
@Cypherpotato Edited
– Patrick Perdigão