Académique Documents
Professionnel Documents
Culture Documents
novaLinha = tabela.NewRow();
//novaLinha[0] = "Paulo Neves";
//novaLinha[1] = 22;
novaLinha["nome"] = "Paulo Neves";
novaLinha["idade"] = 22;
tabela.Rows.Add(novaLinha);
novaLinha = tabela.NewRow();
//novaLinha[0] = "Luís Couto";
//novaLinha[1] = 30;
novaLinha["nome"] = "Luís Couto";
novaLinha["idade"] = 30;
tabela.Rows.Add(novaLinha);
1
2º Exercício de treino
Com listbox
public frmPrincipal()
{
InitializeComponent();
// constroi a tabela
tabela = new DataTable();
// estrutura da tabela
tabela.Columns.Add("nome", typeof(string));
tabela.Columns.Add("idade", typeof(int));
}
2
{
lista.Items.Clear(); // limpa a lista
3
Simplificando o Exercício
public frmPrincipal()
{
InitializeComponent();
// constroi a tabela
tabela = new DataTable();
// estrutura da tabela
tabela.Columns.Add("nome", typeof(string));
tabela.Columns.Add("idade", typeof(int));
}
// adicionar novLinha
DataRow novaLinha = tabela.NewRow();
// buscar os dados
novaLinha["nome"] = txtNome.Text;
novaLinha["idade"] = Convert.ToInt16(txtIdade.Text);
tabela.Rows.Add(novaLinha);
4
{
lista.Items.Add(linha["nome"] + " tem " + linha["idade"]);
}
// apresentar nº de registos
lblResultados.Text = "Registos: " + lista.Items.Count.ToString();
5
Com dataGridView
A propriedade AllowUserToAddRows da dataGrid coloca-se false
public frmPrincipal()
{
InitializeComponent();
// constroi a tabela
tabela = new DataTable();
// estrutura da tabela
tabela.Columns.Add("nome", typeof(string));
tabela.Columns.Add("idade", typeof(int));
}
6
Criar e Comunicar com base de dados
Três passos fundamentais: