想要以另一种形式访问DataGridview的Datasourse属性 [英] Want to access Datasourse property of DataGridview in another form

查看:70
本文介绍了想要以另一种形式访问DataGridview的Datasourse属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海..

我正在使用一个MDI窗体和一个Windows窗体.MDI窗体包含一个datagridview,其中包含来自Database(SQL)的记录.Windows窗体包含保存"按钮[向数据库中添加新数据].我想在单击时在datagridview中显示记录保存按钮.我将Datagridview的修改器设置为Public,但是我无法访问数据源属性.因此,先生,请帮助我.


与谢谢
Sujith

Hai..

I am using One MDI form and One windows form.MDI form contain one datagridview which contain records from Database(SQL).The Windows form contain Save Button[Adding new data to database].I want to Show the record in datagridview when i click save button.I made modifier of Datagridview as Public,but i can not access datasource property.So please help me sir.


With Thank you
Sujith

推荐答案

讨厌,讨厌,讨厌.

public 放回private

当您直接从另一个表单访问表单UI元素(例如DatGridView)时,您会将两个表单锁定在一起:如果不考虑一个表单对另一个表单的影响,就无法更改一个表单.这违反了OOP的原则之一:封装.
这是一个非常糟糕的主意!

而是以提供适当格式的数据源的形式创建public属性:字符串,DataTable或任何其他格式.这样,您可以在不影响外部环境的情况下更改表单的工作方式:只需将属性的输出保持不变.
Nasty, nasty, nasty.

Put that public back to private!

When you access form UI elements such as a DatGridView directly from another form, you are locking the two forms together: you cannot change one without considering it''s effects on teh other. This is against one of the principles of OOP: Encapsulation.
And a very bad idea!

Instead, create a public property in the form which supplies the datasource in an appropriate format: string, DataTable or whatever. That way, you can change the way your form works without affecting the outside world: you just have to keep the output of your property the same.


您好,sujithmtla,

让MDI表单引用Form1,而Windows表单引用Form2,

首先,您必须在form2中创建一个重载的构造函数,并在form1中创建一个全局对象,您已经存在的默认构造函数为:

Hi sujithmtla,

Let MDI form refers to Form1 and Windows form refers to Form2,

first you must create an overloaded constructor in form2 and a global object of form1 , your already existing default constructor is :

public Form2()
{
     InitializeComponent();
}



全局对象和重载的构造函数代码将是:



The global object and the overloaded constructor code will be :

public static Form1 Frm1 = new Form1();

public Form2(Form1 form)
{
     InitializeComponent();
     Frm1 = form;
}



现在,您可以在form1的修饰符中将datagridview设置为public.

然后在点击添加按钮事件中,您将插入以下代码以打开form2:



Now you can set your datagridview in form1 ''s modifier to public .

Then in the click add button event you will insert this code for opening form2 :

Form2 Frm2 =new Form2(this);
Frm2.show();



我希望这次很清楚,
祝你好运
:)



I Hope this time it is clear ,
Good luck
:)



试试这个回调的想法.因此,您将不会访问form1以外的任何内容,而要让form1进行工作.
http://www.codeproject.com/Tips/138326/Use-of-delegates-for-form-reusability-Dependency-i.aspx
Hi
Try this callback idea. So you won''t access anything outside the form1, but let the form1 do its work.
http://www.codeproject.com/Tips/138326/Use-of-delegates-for-form-reusability-Dependency-i.aspx


这篇关于想要以另一种形式访问DataGridview的Datasourse属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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