如何将一个Datagridview的值传递给其他 [英] How Do I Pass Value Of One Datagridview to Other

查看:71
本文介绍了如何将一个Datagridview的值传递给其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Form1 Datagridview中添加了两个表单Form1和Form2,当我点击索引[0,0]的第一个单元格时,另一个表单打开,即Form2,然后在Form2中,Datagridview中有行数现在我希望当我点击Form2 Datagridview中的任何单元格时,该单元格的值将打印在Form1 Datagridview中。



点击此处查看图片



I have two forms "Form1" and "Form2" in Form1 Datagridview is added and when i click on 1st cell at index[0,0] another form open namely "Form2" then in Form2 there was number of row in Datagridview now i want that when i click on any cell in Form2 Datagridview the value of that cell will be printed in the Form1 Datagridview.

Click hereto see image

Purchase pp = new Purchase();    
pp.ValGet(dataGridView1.Rows[0].Cells[0].Value.ToString());
this.Dispose(); 





以上代码是用Form2编写的,这个传输Form1中ValGet()方法中特定单元格的值现在可以告诉我我如何在form1中的Datagridview中设置此值



above code is written in Form2 and this transfer the value of specific cell in ValGet() method present in Form1 now can you tell me how i set this value in Datagridview in form1

推荐答案

嘿只是建议(如RyanDev所说,使用参考),

1.put Form2中的构造函数接受Form1的对象(在类级别将另一个Form1对象声明为public,并将传递的对象分配给构造函数内的对象),

2.当您创建Form2时,使用该构造函数并传递Form2对象(即:this),

并打开Form2作为对话框。

3.And,在CellDoubleClick事件中,使用公共Form2对象访问它的内容(在FOrm1中设置所需的单元格值)。

,你就完成了。

*我建议不要在CellDoubleClick事件中声明Form1的新对象,因为它创建了一个新实例,而不是你创建的实例。



公共购买购买=新购买();



Hey just suggesting(as RyanDev said, use a reference),
1.put a constructor in Form2 to accept a object of Form1(declare another Form1 object as public in class level, and assign passed object to that inside the constructor),
2.when you create Form2, use that constructor and pass along Form2 object(ie: "this"),
and open Form2 as a dialog.
3.And, in the CellDoubleClick event, use public Form2 object to access it's contents(set the required cell value in FOrm1).
and you are done.
*I suggest not to declare a new object of Form1 in the CellDoubleClick event, because it creates a new instance, not the one you had.

public Purchase purchase = new Purchase();

//1.
public ViewStock(Purchase pp) //this is a new constructor
{
     InitializeComponent();
     xConnection = new SqlConnection("Data Source =.; DataBase=AccDB; UID=sa;                                                                                       Password=123;");
     purchase = pp; 
}

//2.
private void xDT_CellEnter(object sender, DataGridViewCellEventArgs e)
{
     if (e.ColumnIndex.Equals(0)) { ViewStock vs = new ViewStock(this); vs.ShowDialog(); }            
}

//3.
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
     //Purchase pp = new Purchase();
     //no need to use above object, since we have the global object "purchase" 

     //use "purchase" object to set your selected value to wherever you want.
     this.Dispose();   
}


给予 ChildForm ParentForm 的引用,如前所述,是一个强烈耦合两种形式的方法。 ChildForm必须了解ParentForm的内部工作才能正确更新。



这很糟糕。



我是一个类似的解决方案,实现起来有点简单,知识逆转了。这也有点好,因为儿童形式往往比父母形式更简单。因此,ParentForm必须对Childform的了解较少,反之亦然。



请参阅:
Giving ChildForm a reference to ParentForm, as proposed earlier, is a way to strongly coupling the two forms. ChildForm has to have knowledge about the inner workings of ParentForm to update it correctly.

This is bad.

I'm for a similar solution, a little simpler to implement and the knowledge reversed. This is also a little better since child forms tend to be simpler than parent forms. Therefore ParentForm has to have less knowledge of Childform as would be the other way round.

See this:
class ParentForm : Form
{
    void GetDataFromChild()
    {
        ChildForm child = new ChildForm();
        DialogResult dialogResult = child.ShowDialog();

        if(dialogResult == DialogResult.Ok)
        {
            pp.ValGet(child.Value);
        }

        child.Dispose();
    }
}

class ChildForm : Form
{
    internal float Value {get; private set;}

    void CellClick(object sender, EventArgs e)
    {
        Value = GetCellValue(e);
        this.DialogResult = DialogResult.Ok;
    }
}

在WinForms中,设置表单的DialogResult属性也会导致关闭它。因此,控制权将返回给ParentForm,后者根据DialogResult行事。关闭 ChildForm 后,可以读取值,直至其处理完毕。

In WinForms, to set the DialogResult property of a form also causes closing it. So control is given back to ParentForm, which acts according to DialogResult. Value can be read after the closing of ChildForm until its disposal.


这篇关于如何将一个Datagridview的值传递给其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆