BANNER

Seo Services

Llenar DGV desde arreglos con VB

Llenar DGV desde arreglos con VB

Esta fue una tarea de la universidad, aquí se digitará la información en tres textbox y luego al presionar el botón de agregar datos estos aparecerán en el DatagridView, sin necesidad de guardarse en ninguna base de datos, a la vez que mostrara en una columna del DataGridView el total de la multiplicación de los valores numericos especificados en cantidadTxT y valorTxT, aquí el diseño:






El código en VB 

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.GenerarColumnas()
    End Sub
    Private ArregloDGV(0 To 3) As DataGridViewColumn 'Arreglo que contendrá la definición de las columnas.
    Private Sub GenerarColumnas()
        ArregloDGV(0) = New DataGridViewTextBoxColumn()
        ArregloDGV(0).HeaderText = "Articulo"
        ArregloDGV(0).Name = "colDesc"
        ArregloDGV(0).Width = 210
        ArregloDGV(1) = New DataGridViewTextBoxColumn()
        ArregloDGV(1).HeaderText = "Cantidad"
        ArregloDGV(1).Name = "colCant"
        ArregloDGV(1).Width = 80
        ArregloDGV(2) = New DataGridViewTextBoxColumn()
        ArregloDGV(2).HeaderText = "$ Valor"
        ArregloDGV(2).Name = "colValor"
        ArregloDGV(2).Width = 80
        ArregloDGV(3) = New DataGridViewTextBoxColumn()
        ArregloDGV(3).HeaderText = "$ Total"
        ArregloDGV(3).Name = "colTotal"
        ArregloDGV(3).Width = 80
        DataGridView1.Columns.AddRange(ArregloDGV)
    End Sub
    Private Sub Agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Agregar.Click
        If NombreTxT.Text.Trim() <> String.Empty AndAlso _
           cantidadTxT.Text.Trim() <> String.Empty AndAlso _
           valorTxT.Text.Trim() <> String.Empty AndAlso _
           IsNumeric(cantidadTxT.Text.Trim()) AndAlso _
           IsNumeric(valorTxT.Text.Trim()) Then
            Dim total As Double = Val(cantidadTxT.Text.Trim()) * Val(valorTxT.Text.Trim())
            Dim arrDatos() As Object = {NombreTxT.Text.Trim(), Val(cantidadTxT.Text.Trim()), _
                                        valorTxT.Text.Trim(), total}
            DataGridView1.Rows.Add(arrDatos)
        End If
    End Sub
End Class

El código en C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private DataGridViewColumn[] ArregloDGV = new DataGridViewColumn[4];
        int cantidad =0;
int valor = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.GenerarColumnas();
        }
        private void GenerarColumnas()
        {
            ArregloDGV[0] = new DataGridViewTextBoxColumn();
            ArregloDGV[0].HeaderText = "Articulo";
            ArregloDGV[0].Name = "colDesc";
            ArregloDGV[0].Width = 210;
            ArregloDGV[1] = new DataGridViewTextBoxColumn();
            ArregloDGV[1].HeaderText = "Cantidad";
            ArregloDGV[1].Name = "colCant";
            ArregloDGV[1].Width = 80;
            ArregloDGV[2] = new DataGridViewTextBoxColumn();
            ArregloDGV[2].HeaderText = "$ Valor";
            ArregloDGV[2].Name = "colValor";
            ArregloDGV[2].Width = 80;
            ArregloDGV[3] = new DataGridViewTextBoxColumn();
            ArregloDGV[3].HeaderText = "$ Total";
            ArregloDGV[3].Name = "colTotal";
            ArregloDGV[3].Width = 80;
            dataGridView1.Columns.AddRange(ArregloDGV);
        }
        private void Agregar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(NombreTxT.Text.Trim()) ||
       string.IsNullOrEmpty(cantidadTxT.Text.Trim()) ||
       string.IsNullOrEmpty(valorTxT.Text.Trim()) ||
       (!int.TryParse(cantidadTxT.Text.Trim(), out cantidad)) ||
       (!int.TryParse(valorTxT.Text.Trim(), out valor)))
{
       return;
}
double total = cantidad * valor;
                object[] arrDatos = {
                           NombreTxT.Text.Trim(),
                           Convert.ToInt32(cantidadTxT.Text.Trim()),
                           valorTxT.Text.Trim(),
                           total
                    };
                dataGridView1.Rows.Add(arrDatos);
            }
        }
   
    }

No hay comentarios