繁体   English   中英

如何在C#中将文本从From2传输到Form1中的dataGridView单元格

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

我有一个包含带按钮单元格的dataGridView的表单。 我还有第二个表单,其中有一个textBox。 如何将文本从Form2传输到Form1上的dataGridView?

例如:

我单击dataGridView按钮单元格以启动第二个窗体,在第二个窗体中,我选择单选按钮以处理来自其中的文本,然后单击一个按钮以将文本传输到Form1中dataGridView中单击的单元格。

这是我到目前为止的代码:

表格1(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;
    }
  }
}

这是我的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;
        }
    }
  }
}

现在一切正常,直到我单击Form2上的按钮以传输文本。 它显示以下错误:

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

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

我到底在做什么错?

谢谢您的帮助。

实际上,解决方案非常简单,您可以更改:

private Top_Shine_Form frm = new Top_Shine_Form();

至:

public static string passingText;

Form2中的按钮:

passingText = radioButton1.Text;

在dataGridView1_CellContentClick的Form1中:

dataGridView1.CurrentCell.Value = Form2.passingText;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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