其他格式的datagridview无法刷新 [英] datagridview which is in other form can't refresh

查看:84
本文介绍了其他格式的datagridview无法刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Windows窗体(Form1和Form2). Form1包含一个按钮和datagridview. Form1显示客户列表. Form2包含4个文本框. Form2是我的客户信息编辑屏幕.我有2个问题:


1)当我按下Form1中的按钮时,我想在文本框中查看数据.
2)当我更改任何textbox.Text内容并关闭Form2时; datagridview中的数据不会更改.当我重新启动项目时,datagridview中的所有行都不会更改.

要在编辑客户列表时打开更多内容,我想立即在datagridview中看到它.我希望刷新它并查看编辑后的数据.(通过我尝试使用datagridview.Refresh()等的方式,但是它不起作用)

I have two Windows Forms (Form1 and Form2). Form1 includes one button and datagridview. Form1 shows customers list. Form2 includes 4 textbox. Form2 is my customer informations edit screen. I have 2 question :


1) When I press button which is in Form1 I want to see datas in textboxes.
2) When I change any textbox.Text content and closed Form2 ; datas which is in datagridview are not changed.When I restart project no problem all rows in datagridview are changed.

To open more when i edit the customers list i want to see it in the datagridview immediately. i want it to be refreshed and see the edited data.(by the way I tried to use datagridview.Refresh() and etc. but it doesn''t worked)

推荐答案

尝试一下:-

首先在form1类中为例如
Try this :-

First create 4 Public Static string variables in form1 class for eg.
public static string text1, text2, text3, text4;


然后在Form1 Button Click事件上编写以下代码:-


then on Form1 Button Click event write the following code:-

private void button1_Click(object sender, EventArgs e)
{
    text1 = Datagridview.Rows[e.RowIndex].Cells[0].value.ToString();
    text2 = Datagridview.Rows[e.RowIndex].Cells[1].value.ToString();
    text3 = Datagridview.Rows[e.RowIndex].Cells[2].value.ToString();
    text4 = Datagridview.Rows[e.RowIndex].Cells[3].value.ToString();
}


然后在Form2中,在表单加载事件或创建按钮上编写以下代码,然后在click事件中编写:-


And in Form2 write following code on form load event or create button and write in click event :-

private void form2_Load(object sender, EventArgs e)
{
   TextBox1.Text = Form1.text1;
   TextBox2.Text = Form1.text2;
   TextBox3.Text = Form1.text3;
   TextBox4.Text = Form1.text4;
}


这是第一个问题的答案.

第二个问题的答案是:-
在Form2中,如果您更改文本框"中的值,则将其保存.仅在保存按钮单击"事件中,保存代码"完成后,调用Form1 Load事件或您在其中编写代码的任何其他事件中将数据加载到Datagridview中.
在Form1中,如果您已在Form Load事件中编写了代码以在Datagridview中加载数据,则只需复制该代码并创建一个Public方法,如下所示:-


This is the Answer for your First Question.

Second Question Answer is :-
In Form2 if you change the values in Textbox then you save it. In Save Button Click event only, after the Save Code is complete, call the Form1 Load event or any other event in which you have written the code to load data in Datagridview.
In Form1 if you have written Code in Form Load event to Load data in Datagridview, then just copy that code and create One Public method like this:-

public void Get_Data()
{
    //Code to Load data in Datagridview.
}


并在Form Load事件中调用此方法,如下所示:-


and Call this method in Form Load event like this :-

private void form1_Load(object sender, EventArgs e)
{
   public void Get_Data();
}


并以相同的方式在Form 2 Save Button Click事件中调用相同的方法,如下所示:-
首先创建一个表单对象,您已经在其中编写了代码和访问方法,如下所示.


And in same way call the same method in Form 2 Save Button Click event like this:-
First create the form object in which you have written code and access method, like this.

private void Savebutton_Click(object sender, EventArgs e)
{
    //First write code to save text box data that you have already written
    Form1 f1 = new Form1();
    f1.Get_Data();
}



这是第二个问题的答案

如果需要任何帮助,请在评论或回复中发帖.



This is The Answer for Second Question

If need any help then plz post in comment or reply.


这篇关于其他格式的datagridview无法刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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