Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to add multiple rows by clicking the button

form1

C#
using System;

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 static List<employee> lst = new List<employee>();
        public Form1()
        {
            InitializeComponent();
        }      
        private void button1_Click(object sender, EventArgs e)
        {
           employee p = new employee();
            p.eid = Convert.ToInt32(textBox1.Text);
            p.ename = textBox2.Text;
            p.edes = textBox3.Text;
            Form2 form2 = new Form2();
            form2.passThisValuetoDgv(p.eid, p.ename, p.edes);
            form2.ShowDialog();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            employee p = new employee();
            p.eid = Convert.ToInt32(textBox1.Text);
            p.ename = textBox2.Text;
            p.edes = textBox3.Text;
            lst.Add(p);
            dataGridView1.DataSource = lst;
           
        } 
    }
    public  class employee
        {
            public int eid { get; set; }
            public string ename { get; set; }
            public string edes { get; set; }
        }
}

form2


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 Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
        DataTable dt;
        public void passThisValuetoDgv(int value1, string value2,string value3)
        {
            dt = new DataTable();
            dt.Columns.Add("Col1");
            dt.Columns.Add("Col2");
            dt.Columns.Add("Col3");
            dataGridView1.DataSource = dt;

            dt.Rows.Add(value1);
            dataGridView1.CurrentRow.Cells[1].Value = value2;
            dataGridView1.CurrentRow.Cells[2].Value = value3;


        }
        }


    }
Posted
Updated 19-Feb-13 0:55am
v4
Comments
Simon_Whale 19-Feb-13 5:54am    
why have you removed most / all of your question?
Member 9553202 19-Feb-13 6:13am    
actually the value is to be added to the grid view when enter the text in textbox n click add button
Simon_Whale 19-Feb-13 6:19am    
if you want to add then straight to the dataGridView from the Form then I would do something like this

dataGridView1.RowsAdd(new object[] { value1, value2, value3});

1 solution

Your problem is here

C#
dt.Rows.Add(value1);
dataGridView1.CurrentRow.Cells[1].value = value2;
dataGridView1.CurrentRow.Cells[2].value = value3;



if you want to add rows by using values direct on the grid have a look at this

How can I manually add data to a datagridview[^]

personally I would prefer to add the row to the datatable as it will be easier to do other logic to that collection later How to: Add Rows to a DataTable[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900