简体   繁体   中英

How to transfer text from From2 to dataGridView cell in Form1 in C#

I have a form that contains dataGridView with button cells. I also have a second form that has a textBox. how do I transfer the text from Form2 to the dataGridView on Form1?

for example:

I click on the dataGridView button cell to launch the second form, in the second form I select a radioButton to cope the text from and then click a button to transfer the text to the clicked cell in the dataGridView in Form1.

This is the code that I have so far:

Form1(Top_Shine_Form):

namespace Top_Shine
{
public partial class Top_Shine_Form : Form
{
    public Top_Shine_Form()
    {
        InitializeComponent();
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if(e.ColumnIndex >= 2)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (radioButton1.Checked)
        {
            DataTable dgv1 = new DataTable();
            dgv1.Columns.Add("Time");
            dgv1.Columns.Add("CarColorNumber");
            dgv1.Columns.Add("Interior");
            dgv1.Columns.Add("Exterior");

            DataRow row = dgv1.NewRow();
            row["Time"] = Timetxt.Text;
            row["CarColorNumber"] = CNametxt.Text + " / " + CColortxt.Text + " / " + CNumbertxt.Text;

            row["Interior"] = "*";
            row["Exterior"] = "*";

            dgv1.Rows.Add(row);

            foreach (DataRow dr in dgv1.Rows)
            {
                int num = dataGridView1.Rows.Add();
                dataGridView1.Rows[num].Cells[0].Value = dr["Time"].ToString();
                dataGridView1.Rows[num].Cells[1].Value = dr["CarColorNumber"].ToString();

                if (interiorCB.Checked)
                {
                    dataGridView1.Rows[num].Cells[2].Value = dr["Interior"].ToString();
                }
                if (ExteriorCB.Checked)
                {
                    dataGridView1.Rows[num].Cells[3].Value = dr["Exterior"].ToString();
                }

            }
            radioButton1.Checked = false;
        }

        CNametxt.Clear();
        CColortxt.Clear();
        CNumbertxt.Clear();
        Timetxt.Clear();
        interiorCB.Checked = false;
        ExteriorCB.Checked = false;
    }
  }
}

This is my code for Form2:

namespace Top_Shine
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private Top_Shine_Form frm = new Top_Shine_Form();
    private void button1_Click(object sender, EventArgs e)
    {
        int num = frm.dataGridView1.Rows.Add();
        if (radioButton1.Checked)
        {
            frm.dataGridView1.CurrentCell.Value = radioButton1.Text;
        }
    }
  }
}

Now everything runs fine until I click the button on Form2 to transfer the text. and it shows the following error:

An unhandled exception of type 'System.NullReferenceException' occurred in Top Shine.exe

Additional information: Object reference not set to an instance of an object.

What exactly am I doing wrong?

Thanks for the help.

The solution is very simple actually, you change the:

private Top_Shine_Form frm = new Top_Shine_Form();

To:

public static string passingText;

The button in Form2:

passingText = radioButton1.Text;

In Form1 on dataGridView1_CellContentClick:

dataGridView1.CurrentCell.Value = Form2.passingText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM