Insert values from textbox to a datagridview

Satyapriyanayak
Posted by Satyapriyanayak under C# category on | Points: 40 | Views : 7553
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 = "";
}

}
}

Comments or Responses

Login to post response