Automatic row addition in gridview on button click
I have two textbox, one button and one gridview. i want that when i click on button, two textbox values will be added to gridview's 1st row. then when i again click on the button, two textbox values will be added to the gridview's 2nd row and so on... I am able to add gridview's 1st row by clicking button, but unable to do further. please help me on these by checking below attached code.

Posted Aug 5, 2013, 3:22 PM
I changed your.
You should not directly change the cells values, instead of that, you need add the records in your data source and rebind the grid once again.
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 GridviewExample
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["Name"] = textBox1.Text;
dr["Address"] = textBox2.Text;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
}
}
Hope this helps you.
Preeti AgrawallaPosted Aug 6, 2013, 1:10 AM