In this blog we will know how to insert multiple values from textbox and show them into a datagridview.
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
Add_multiple_rows_to_datagridview
{
public partial
class Form1 :
Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt = new
DataTable();
DataRow dr;
private void
btn_add_Click(object sender,
EventArgs e)
{
dr =
dt.NewRow();
dr["Name"] = txt_name.Text;
dr["Address"] = txt_address.Text;
dr["Age"] = txt_age.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
clear();
}
private void
Form1_Load(object sender,
EventArgs e)
{
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Age");
}
private void
clear()
{
txt_name.Text = "";
txt_address.Text = "";
txt_age.Text = "";
}
}
}

Virender KumarPosted Aug 5, 2018, 6:57 AM
I got error : - column 'LoonNo.' doesn't belong to table. in below code
Virender KumarPosted Aug 5, 2018, 6:56 AM
Namespace IncentiveCalculator{ public partial class frmInCal : Form { public frmInCal() { InitializeComponent(); } DataTable dt = new DataTable(); DataRow dr; private void Form1_Load(object sender, EventArgs e) { comboBox1.Items.Add("A"); comboBox1.Items.Add("B"); comboBox1.Items.Add("C"); comboBox1.Items.Add("GEN"); comboBox1.SelectedItem = "A"; cbLoomType.Items.Add("Small"); cbLoomType.Items.Add("Large"); cbLoomType.SelectedItem = "Small"; } private void btnExit_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Are you sure?", "Incentive Calculator", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { this.Close(); } else { } } private void btnAdd_Click(object sender, EventArgs e) { dr = dt.NewRow(); dr["LoomNo."]=txtLoomNo.Text; dr["LoomType"]=cbLoomType.SelectedItem; dr["WeaverID"]=txtWId.Text; dr["QualityCode"]=txtQCode.Text; dr["Points"]=txtPoints.Text; dt.Rows.Add(dr); DataGrid1.DataSource = dt; txtLoomNo.Text = ""; txtWId.Text = ""; txtQCode.Text = ""; txtWId.Text = ""; } } }
Virender KumarPosted Aug 5, 2018, 6:55 AM
I got below error:-
Divya DiyaPosted Jan 28, 2018, 11:45 PM
Its quite easy for beginners to understand
Nithin VarghesePosted Nov 15, 2017, 3:12 AM
Its good and very helpful
GokulPosted May 27, 2016, 2:09 AM
it's good
Nilesh JadavPosted Jun 20, 2015, 6:52 AM
Nice one