如何在从其他表单进行更改时更改datagridview的数据 [英] How to change the data of datagridview when changes has made from another form

查看:63
本文介绍了如何在从其他表单进行更改时更改datagridview的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows应用程序。


我有两种形式,Form1和Form2。它们都是开放的。


在Form1中,我有两个dateTimePickers(开始日期和结束日期),两个文本框(没有记录和没有日期)和两个按钮(提交和取消)。在Form2中,我有一个显示文本文件数据的dataGridView。


例如,我将form1的开始日期设置为2010年5月5日,结束日期为2010年5月6日单击提交按钮后,form2将读取文本文件并显示> =开始日期和< =结束日期的数据。

解决方案

< blockquote class =post_quotes> 原创海报:如何让我的Form2对我的Form1上的内容作出反应?

如何让我的Form1控制我的Form2上的内容?



虽然你可以让Form1直接访问Form2上的项目,但这不是推荐的方法。它将两种形式紧密地联系在一起。如果在Form2中进行了更改,例如删除其中一个控件,则Form1中的代码会中断。

Form1最好引发一个事件,并让Form2包含自己的代码以便如何应对这个。这将Form2中的Form2和Form1中的Form1负责。

它使Form1幸福地无知Form2 - 以及一个日志组件 - 和一个进度组件,还有十几个可以订阅的小黑盒子对于Form1中的事件,所有没有Form1负责直接影响除自身以外的控件。
活动教程(包括Form to Form,与班级相同)
这个收银机教程就是这样:它制作一个虚拟数字键盘。


错误:直接访问一个类/表格的控件从另一个。

展开 | 选择 | Wrap | 行号


@tlhintoq


我很感谢你对此事的回复..

你能给我一些可以帮我解决问题的代码吗?


这是什么我目前有:


//当点击提交按钮时是Form1

展开 | 选择 | 换行 | 行号


当您发布导致问题的实际代码时,它会极大地帮助志愿者。不是整个应用程序。只是你写的导致问题的部分。


1 - 从Visual Studio复制代码




2 - 在您的问题主题中,单击[代码]标签按钮。它是一个看起来像#symbol



代码标签神奇地出现在你的帖子中,光标位于它们之间。


[code] | [/ code]


只需粘贴即可看到这个

[code] public void BrokenMethod()

{

//我不明白为什么这不起作用

int Yogi ="熊;

} [/ code]


这将是这样的

展开 | 选择 | Wrap | 行号

I''m currently developing a windows application.

I have two forms, Form1 and Form2. They are both open.

In Form1, I have two dateTimePickers (start date and end date), two text boxes (no of records and no of days) and two buttons (submit and cancel). In Form2, I have a dataGridView that displays data from a text file.

For example, I set the start date from form1 as May 5, 2010 and end date as May 6, 2010. After i click the submit button, form2 will read the text file and display the data that are >= start date and <= end date.

解决方案

Original Poster: How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?

Although you can have Form1 directly access items on Form2 it isn''t the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.

Bad: Directly accessing controls of one class/form from another.

Expand|Select|Wrap|Line Numbers


@tlhintoq
I appreciate your reply regarding this matter..
Can you give me the code that can help me solve the problem?

This is what I currently have:

//when Submit button is click is Form1

Expand|Select|Wrap|Line Numbers


It helps the volunteers here tremendously when you post the actual code that is causing you problems. Not the entire application. Just the parts you have written that are causing the problem.

1 - Copy the code from Visual Studio



2 - In your question thread, click the [code] tags button. Its the one that looks like # symbol


Code tags have magically appeared in your post with the cursor right between them.

[code]|[/code]

Just paste and you should see this
[code] public void BrokenMethod()
{
// I don''t understand why this doesn''t work
int Yogi = "Bear";
}[/code]

Which will look like this

Expand|Select|Wrap|Line Numbers


这篇关于如何在从其他表单进行更改时更改datagridview的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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