Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to insert data into datagridview from antoher form, as an illustrated like this, i have datagridview in form1 and textbox in form2..in form2 i have input button. everytime i clicked the input button the textbox value from form1 will inserted into datagridview in form1. My question is how i can make new rows in datagridview after i have clicked the input button from form2?

Please Help me.
Posted
Updated 14-Nov-20 6:16am

Create an event in Form2, and handle it in Form1.
When you click the button in Form2, package the data and add it to a custom EventArgs which you pass with the event when you signal it.
In Form1, when the event occurs, read the EventArgs in the handler and add it to the DataGridView as a new row.
Form2:

C#
public partial class frmOther : Form
    {
    public frmOther()
        {
        InitializeComponent();
        }

    public event EventHandler<ChangedArgs> Changed;

    private void butGo_Click(object sender, EventArgs e)
        {
       EventHandler ch = Changed;
       if (ch != null)
          {
          ch(this, new ChangedArgs(tbData.Text));
          }
        }
    }

public partial class ChangedArgs : EventArgs
    {
    public string strData;
    public ChangedArgs(string str)
        {
        strData = str;
        }
    }

Form1:
C#
frmOther otherForm = new frmOther();

private void frmTextBox_Load(object sender, EventArgs e)
    {
    otherForm.Changed += new EventHandler<ChangedArgs>(Changed);
    otherForm.Show();
    }

private void Changed(object sender, ChangedArgs e)
    {
    if (e != null)
        {
        string myDataFromTheOtherForm = e.strData;
        }
    }
 
Share this answer
 
VB.Net: On the click event of button on form2 try adding
Form1.DataGridView1.Rows.Add(Me.TextBox1.Text)
Hope this helps.
 
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