DatagridView en C# [08]
En este tutorial manejaremos registros en una tabla o DatagridView en C# siguiendo estos pasos:
- Insertamos un dataGridView tres textBox y dos botones.
- Al datagrid le colocamos tres columnas con Nombre, Apellido y Edad.
- A los textBox les colocamos los nombres txtNombre, txtApellido y txtEdad.
- Un botón será para agregar y el otro botón será para borrar registros.
Codigo de un DatagridView en C#: https://github.com/programadornovato/AplicacionWindowsForm/commit/436cbb3a41899bc47be81614318b30e398c30df5
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace AplicacionWindowsForm
{
public partial class Ventana2 : Form
{
int con = 0;
int sel;
public Ventana2()
{
InitializeComponent();
}
private void btnAgregar_Click(object sender, EventArgs e)
{
int n = dgPersonas.Rows.Add();
dgPersonas.Rows[n].Cells[0].Value = txtNombre.Text;
dgPersonas.Rows[n].Cells[1].Value = txtApellido.Text;
dgPersonas.Rows[n].Cells[2].Value = txtEdad.Text;
txtNombre.Text = "";
txtApellido.Text = "";
txtEdad.Text = "";
}
private void dgPersonas_CellClick(object sender, DataGridViewCellEventArgs e)
{
sel = e.RowIndex;
if (sel != -1) {
string nombre = dgPersonas.Rows[sel].Cells[0].Value.ToString();
string apellido = dgPersonas.Rows[sel].Cells[1].Value.ToString();
string edad = dgPersonas.Rows[sel].Cells[2].Value.ToString();
lblValor.Text = nombre +" "+ apellido + " " + edad;
}
}
private void btnBorrar_Click(object sender, EventArgs e)
{
if (sel != -1)
{
dgPersonas.Rows.RemoveAt(sel);
}
}
}
}
Descargar Visual Studio 2019: https://visualstudio.microsoft.com/es/downloads/
[CURSO] C##️⃣: https://www.youtube.com/watch?v=NKPMGY6NCko&list=PLCTD_CpMeEKQSOU8Vf9VHXrZa2rc8X0X5&ab_channel=programadornovato
[CURSO] C++ DE 0 A HEROE 🦸: https://www.youtube.com/watch?v=APN8aCyPvww&list=PLCTD_CpMeEKTofxs7iottRxJ5YPM7BOcc&ab_channel=programadornovato
[Curso] Java Netbeans GUI Completo☕: https://www.youtube.com/watch?v=18UA7X2ss8g&list=PLCTD_CpMeEKThfXo8D-RXOGu5FarO7_qv&ab_channel=programadornovato
🔗 Canal de ProgramadorNovato: https://www.youtube.com/c/programadornovato
🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
🔗 Pagina oficial: https://www.programadornovato.com
🔗 Gracias por apoyar este canal: https://www.patreon.com/programadornovato