C# - How To Insert Data Using InputBoxes Into DataGridView In C#
In This C# Code We Will See How To Add A Row To DataGridView From InputBoxes
In C# Programming Language.
Source Code:
In C# Programming Language.
Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication1
{
public partial class Add_Row_DatagridView_Using_InputBox_Csharp : Form
{
public Add_Row_DatagridView_Using_InputBox_Csharp()
{
InitializeComponent();
}
DataTable table = new DataTable();
private void Add_Row_DatagridView_Using_InputBox_Csharp_Load(object sender, EventArgs e)
{
table.Columns.Add("Id", typeof(int));
table.Columns.Add("First Name", typeof(String));
table.Columns.Add("Last Name", typeof(String));
table.Columns.Add("Age", typeof(int));
dataGridView1.DataSource = table;
}
// Button Add Row
private void BtnAddRow_Click(object sender, EventArgs e)
{
// get data from inputboxes
int id = int.Parse(Interaction.InputBox("Enter The Id", "Data", "", -1, -1));
string fn = Interaction.InputBox("Enter The First Name", "Data", "", -1, -1);
string ln = Interaction.InputBox("Enter The Last Name", "Data", "", -1, -1);
int age = int.Parse(Interaction.InputBox("Enter The Age", "Data", "", -1, -1));
table.Rows.Add(id, fn, ln, age);
dataGridView1.DataSource = table;
}
}
}
/////////////////////////////////OUTPUT:
c# add row to datagridview |