如何将值从DataGridView传递到另一个Form文本框

时间:2013-06-30 12:54:43

标签: c# winforms datagridview

我可以转移现在问题所在的值切换情况我使交换机能够将数据传输到几个表格只放置表单打开把我在交换机中不能这样做它在运行时转到默认情况问题是(FORMID)我如何在这里正确使用开关盒 这是我的form1代码

public partial class Form1 : Form
{
    Form2 f2 = new Form2();
    public Form1()
    {
        InitializeComponent();
        f2.setParent(this);
        f2.MdiParent = this.MdiParent;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f2.Show();
        f2.Activate();
    }
}

}

和此表单2代码:

public partial class Form2 : Form
{
    public int FORMID = 0;
    private Form1 f1;
    private Form3 f3;
    DataTable dt;

    public Form2()
    {
        InitializeComponent();
    }

    void load_table()
    {
        string constring = "Data Source =.; initial Catalog = business; Integrated Security=SSPI;";
        SqlConnection CN = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand("select * from T_AKARAT_BUILDING_TP", CN);
        try
        {
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            dt = new DataTable();
            sda.Fill(dt);
            BindingSource bsource = new BindingSource();
            bsource.DataSource = dt;
            dataGridView1.DataSource = bsource;
            sda.Update(dt);
        }
        catch { }
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        load_table();
    }

    public void setParent(Form1 parentValue)
    {
        f1 = parentValue;
    }

    public void setParent(Form3 parentValue)
    {
        f3 = parentValue;
    }

    private void dataGridView1_DoubleClick(object sender, EventArgs e)
    {
        switch (FORMID)
        {
            case 1:

        f1.textBox1.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][0].ToString();
        f1.textBox2.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][1].ToString();
        this.Hide();
        break;
            case 2:
        f3.textBox1.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][0].ToString();
        f3.textBox2.Text = dt.Rows[dataGridView1.CurrentCell.RowIndex][1].ToString();
        this.Hide();
        break;
            default:
        MessageBox.Show("plz select");
        break;
        }
        this.Hide();
    }
}

}

我在表单2中将text框修饰符设置为public,但此代码不起作用&我不知道这里有什么遗漏或错误希望能帮助我

希望有人纠正我的代码而不是提供示例

6 个答案:

答案 0 :(得分:0)

另一种方法是,在form2中定义两个公共变量,并将form1中的这个值设置为那些变量,然后在form2中输入Event将这些变量设置为textboxes

答案 1 :(得分:0)

为此,您可以使用该课程 创建一个class1.cs 类中的代码,例如:

class Class1
{
    public string firstName;
    public string lastName;
    public Class1 FirstName(string firstname)
    {
        this.firstName = firstname;
        return this;
    }
    public Class1 LastName(string lastname)
    {
        this.lastName = lastname;
        return this;
    }
}
form1中的

代码:

Class1 c = new Class1();
private void button1_Click(object sender, EventArgs e)
{
    c.firstName = dataGridView1.CurrentRow.Cells[1].Value.ToString();
    c.lastName = dataGridView1.CurrentRow.Cells[2].Value.ToString();
}

现在您可以将此代码用于所有程序

另一种形式的例子:

label1.text = c.firstName;
label2.text = c.lastName;

祝你好运

答案 2 :(得分:0)

为文本框分配值应该按照您的方式工作。但是你的代码没有调用FDAS.Show。

也是你的结束form1,因此你可能需要声明一个新的form1来设置父

答案 3 :(得分:0)

在cellmoused双击事件中尝试它,我放的名字只是示例,你可以改为自己:

private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
       Form2 f2 = new Form2();
       f2.textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
       f2.Show();
}

如果textbox的访问修饰符是公共的,则会这样做。

这2个示例将您的2个文本框文本放置到1º示例 - 列值 - ,2º示例 - 行值。

        private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            Form2 f2 = new Form2();
            f2.textBox1.Text = dataGridView1.Rows[0].Cells[e.ColumnIndex].FormattedValue.ToString();
            f2.textBox2.Text = dataGridView1.Rows[1].Cells[e.ColumnIndex].FormattedValue.ToString();
            f2.Show();
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            Form2 f2 = new Form2();
            f2.textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
            f2.textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString();
            f2.Show();
        }

答案 4 :(得分:0)

也许这样.. FDAS必须首先声明为Form class

public void setParent()
{
    FDAS = form2;
}

答案 5 :(得分:0)

不幸的是,您没有说明哪一行代码导致“对象引用未设置”异常。而且,由于我们无法看到您的所有代码,因此我们必须猜测问题可能在哪里。

实例化Form2时,必须设置它的MdiParent属性。然而,你在Form2构造函数中使用了那个MdiParent属性(这也不正确):

public form2()
{
    InitializeComponent();
    // neither one of the following lines make any sense, get rid of them
    //form1.setParent(this);
    //form1.MdiParent = this.MdiParent; 
}

您没有显示在Form1中实例化Form2的代码,但需要像这样完成:

// somewhere in Form1
Form2 FDAS = new Form2();
FDAS.MdiParent = this;

另外,因为form1是MdiParent,我认为当你.Close()它时你也会出错。所以我不确定你为什么这样做!