窗口形式和数据网格 [英] window form and datagrid

查看:73
本文介绍了窗口形式和数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有两种形式的窗口应用程序.第一种形式是计算一些值并在4个文本框中显示,现在我想将这些值添加到其他形式的datagrid中.

I''m deveoping a window application ,having two forms. first form is calculating some values and diplaying in 4 text boxes,now i want to add these values to datagrid on other form.

推荐答案

C#中的表单是类的实例他们自己.因此,您可以在关闭表单或切换回其他表单之前将4个变量设为公共变量并设置其值.

Forms in C# are instances of classes themselves. So you could make 4 variables public and set their values before you close the form or switch back to the other form.

class form2
{
public string txtbox1;
public string txtbox2;
public string txtbox3;
public string txtbox4;
...
}



无论您从何处致电此表格.



Wherever you call this form from.

form2 f2 = new form2();

if (f2.ShowDialog() == DialogResult.OK)
{
   //to get variables 
   f2.txtbox1; //etc... to retrive values
}



如果尚不清楚,请告知我,我将尝试进一步解释.



If this is unclear please let me know I will try to explain further.


如果数据网格的源是表,则可能要向表中添加数据并简单地显示网格.
尚不清楚要实现什么目标.
If the source of the data grid is a table, you might want to add data to the table and simply show the grid.
It is not entirely clear what you want to achieve.


您可以将文本框上的"Modifier"属性设置为"Public",以使其可用于其他form2.在form1上这样说

you can set the "Modifier" property on the text boxes to "Public" that would make them available for the other form2 so you could say like this on the form1

Form1 frm = new Form1();
if(frm.ShowDialog()==DialogResult.OK)
{
DataTable dtb=new DataTable();
dtb.Colums.Add("TextBoxValues");

dtb.Rows.Add(frm1.TextBox1.Text);
dtb.Rows.Add(frm1.TextBox2.Text);
dtb.Rows.Add(frm1.TextBox3.Text);
dtb.Rows.Add(frm1.TextBox4.Text);

DataGridView drg=new DataGridView();
drg.DataSource=dtb;
this.Controls.Add(drg);
}


这篇关于窗口形式和数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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