Pegar dados de um grid view e jogar em outro form
14/06/2012
0
Tenho um form: frmUsuario com um cadastro de usuario com um botao btnLocalizar que abre outro form: frmConsultarUsuario onde e listado uma gridview: dgvUsuario com a lista de usuarios cadastrados, no form: frmConsultarUsuario que tem a grid nao consigo listar os dados dos usuarios cadastrado nem fazer uma pesquisa,
oq necessito mesmo e no form: frmConsultarUsuario consigo enxergar os dados e preencher as textbox do form: frmUsuario.
segue o code do form:frmUsuario :
namespace SisCras
{
public partial class frmUsuario : Form
{
public frmUsuario()
{
InitializeComponent();
btnCancelar.Visible = false;
}
RegraNegocio negocio = new RegraNegocio();
private void habilitarControles()
{
txtUsuario.Enabled = true;
txtSenha.Enabled = true;
txtrepitaSenha.Enabled = true;
}
public void LimparCampo()
{
this.LimparCampo(this);
}
public void LimparCampo(Control parent)
{
foreach (Control cont in parent.Controls)
{
if (cont is TextBox)
(cont as TextBox).Text = ;
if (cont is ComboBox)
(cont as ComboBox).Text = ;
if (cont is MaskedTextBox)
(cont as MaskedTextBox).Text = ;
if (cont is RadioButton)
(cont as RadioButton).Checked = false;
if (cont is CheckBox)
(cont as CheckBox).Checked = false;
if (cont is RichTextBox)
(cont as RichTextBox).Text = ;
if (cont is CheckedListBox)
{
foreach (ListControl item in (cont as CheckedListBox).Items)
item.SelectedIndex = -1;
}
if (cont is ListView)
(cont as ListView).Items.Clear();
// varre os filhos...
this.LimparCampo(cont);
}
}
private void btnLocalizar_Click(object sender, EventArgs e)
{
frmConsultarUsuario newfrmConsultarUsuario = new frmConsultarUsuario();
newfrmConsultarUsuario.ShowDialog();
}
private void frmUsuario_Load(object sender, EventArgs e)
{
btnNovo.Enabled = true;
negocio.PreencherDataGrid(dgvUsuario);
}
private void btnNovo_Click(object sender, EventArgs e)
{
if (MessageBox.Show(Deseja inserir um novo usuário?, Mensagem do Sistema,
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
habilitarControles();
btnNovo.Enabled = false;
btnAtualizar.Enabled = false;
btnExcluir.Enabled = false;
btnPrimeiro.Enabled = false;
btnProximo.Enabled = false;
btnUltimo.Enabled = false;
btnAnterior.Enabled = false;
if (txtUsuario.Text != null)
{
txtUsuario.Focus();
btnSalvar.Enabled = true;
btnCancelar.Visible = true;
}
LimparCampo();
}
private void btnCancelar_Click(object sender, EventArgs e)
{
habilitarControles();
btnNovo.Enabled = true;
btnAtualizar.Enabled = true;
btnExcluir.Enabled = true;
btnPrimeiro.Enabled = true;
btnProximo.Enabled = true;
btnUltimo.Enabled = true;
btnAnterior.Enabled = true;
{
txtUsuario.Focus();
btnSalvar.Enabled = true;
btnCancelar.Visible = false;
}
LimparCampo();
}
private void btnSalvar_Click(object sender, EventArgs e)
{
if (txtUsuario.Text != )
{
RegraNegocio cln = new RegraNegocio();
cln.Usuario = this.txtUsuario.Text;
cln.Senha = this.txtSenha.Text;
cln.RepitaSenha = this.txtrepitaSenha.Text;
cln.InserirUsuario();
negocio.PreencherDataGrid(dgvUsuario);
btnSalvar.Enabled = false;
btnAtualizar.Enabled = true;
btnExcluir.Enabled = true;
{
txtUsuario.Focus();
btnSalvar.Enabled = true;
}
LimparCampo();
MessageBox.Show(Usuário cadastrado com sucesso, Sucesso,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
txtUsuario.Focus();
btnNovo.Enabled = true;
btnSalvar.Enabled = true;
btnAtualizar.Enabled = true;
btnExcluir.Enabled = true;
btnPrimeiro.Enabled = true;
btnProximo.Enabled = true;
btnUltimo.Enabled = true;
btnAnterior.Enabled = true;
MessageBox.Show(Adicione um nome de Usuário, Atenção, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void btnAtualizar_Click(object sender, EventArgs e)
{
try
{
if (txtidUsuario.Text != null)
{
negocio.IdUsuario = int.Parse(txtidUsuario.Text);
negocio.Usuario = txtUsuario.Text;
negocio.Senha = txtSenha.Text;
negocio.RepitaSenha = txtrepitaSenha.Text;
negocio.AtualizarUsuario();
negocio.PreencherDataGrid(dgvUsuario);
MessageBox.Show(Atualizado com Sucesso, Sucesso, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(O campo Usuário e obrigatório, Anteção, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(Não foi possivel atualizar os dados pelo seguinte motivo: + ex.Message);
}
}
private void btnExcluir_Click(object sender, EventArgs e)
{
try
{
if (txtidUsuario.Text != null)
{
negocio.IdUsuario = int.Parse(txtidUsuario.Text);
negocio.ExcluirUsuario();
LimparCampo();
negocio.PreencherDataGrid(dgvUsuario);
btnExcluir.Enabled = false;
btnAtualizar.Enabled = false;
btnSalvar.Enabled = true;
MessageBox.Show(Excluido com Sucesso, Sucesso, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(O campo Usuário e obrigatório, Anteção, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(Não foi possivel excluir os dados pelo seguinte motivo: + ex.Message);
}
}
}
}
esta dando erro nesta grid que esta no form frmConsultarUsuario
datagridview: dgvusuario
code do form frmConsultarUsuario:
namespace SisCras
{
public partial class frmConsultarUsuario : Form
{
public frmConsultarUsuario()
{
InitializeComponent();
}
RegraNegocio negocio = new RegraNegocio();
private void btnPesquisar_Click(object sender, EventArgs e)
{
if (cbmFiltrar.Text == IdUsuario)
{
negocio.IdUsuario = int.Parse(mkPesquisar.Text);
negocio.ConsultarId(dgvUsuario);
}
if (cbmFiltrar.Text == Usuario)
{
negocio.Usuario = mkPesquisar.Text;
negocio.ConsultarUsuario(dgvUsuario);
}
if (cbmFiltrar.Text == )
{
negocio.PreencherDataGrid(dgvUsuario);
}
}
private void dgvUsuario_Click(object sender, EventArgs e)
{
habilitarControles();
btnAtualizar.Enabled = true;
btnExcluir.Enabled = true;
btnSalvar.Enabled = false;
txtIdUsuario.Text = dgvUsuario.CurrentRow.Cells[0].Value.ToString();
txtSenha.Text = dgvUsuario.CurrentRow.Cells[1].Value.ToString();
txtRepitaSenha.Text = dgvUsuario.CurrentRow.Cells[2].Value.ToString();
}
}
}
esta dando erros:
habilitarControles
btnAtualizar
btnExcluir
btnSalvar
txtIdUsuario
txtSenha
txtRepitaSenha
Espero que expliquei para o entendimento dos caros amigos, que assim eu venha solucionar esse problema.
Sigrids Lima
Posts
14/06/2012
Joel Rodrigues
Então faz isso, diz pra nós quais erros ocorreram e em quais linhas.
Abraço.
15/06/2012
Sigrids Lima
Error 2 The name btnAtualizar does not exist in the current context frmConsultarUsuario.cs 43 13
Error 3 The name btnExcluir does not exist in the current context frmConsultarUsuario.cs 44 13
Error 4 The name btnSalvar does not exist in the current context frmConsultarUsuario.cs 45 13
Error 5 The name txtIdUsuario does not exist in the current context frmConsultarUsuario.cs 46 13
Error 6 The name txtSenha does not exist in the current context frmConsultarUsuario.cs 47 13
Error 7 The name txtRepitaSenha does not exist in the current context frmConsultarUsuario.cs 48 13
Error 8 The name dgvUsuario does not exist in the current context frmUsuario.cs 74 39
Error 9 The name dgvUsuario does not exist in the current context frmUsuario.cs 129 43
Error 10 The name dgvUsuario does not exist in the current context frmUsuario.cs 171 47
Error 11 The name dgvUsuario does not exist in the current context frmUsuario.cs 194 47
15/06/2012
Joel Rodrigues
15/06/2012
Sigrids Lima
private void frmConsultarUsuario_Load(object sender, EventArgs e)
{
negocio.PreencherDataGrid(dgvUsuario);
}
conseguir resolver boa parte do codigo, so nao estou conseguindo preencher as textbox do frmUsuario,
seria assim quando clickar na grid com a lista de usuarios que esta no frmConsultarusuario
18/06/2012
Sigrids Lima
http://www.linhadecodigo.com.br/artigo/1741/trocando-informacoes-entre-windowsforms-em-csharp.aspx
Fiz umas alterações adicionando mais textbox.
Valeu um abraço
20/06/2012
Joel Rodrigues
Boa sorte.
Clique aqui para fazer login e interagir na Comunidade :)