开发者

get value's from a datagridview from other form

HI,

Iam trying to get value's out of a datagridview. this datagridview is on form one.

and where i want the value's is on form two;

but i dont want to do this :

[code]form1 frm = new from1();[/code]

because t开发者_如何学运维hat form1 already exists so i dont want to create it again

can anytone plz help me get a solution for this thank you very much


Please don't even try to do that. Store your data in a data container object that is shared between the two forms. Bind form1 to the data and access it from form2.


You can access other open forms using the OpenForms collection on Application:

Application.OpenForms

Then all you need to do is test for the type or name of the form and cast it to your second form to grab the reference, then you can access its properties etc.

However, grabbing pieces of information like this across forms is considered bad design. If the information can be aggregated out into something both forms can reference, this is better. Alternatively, if the forms need to interact based on the state of each of their data, consider creating events between the two forms.


in form1.designer.cs we have datagrid

    public System.Windows.Forms.DataGridView GridOgrenci;

and form2 name yetkiler we can reach all form1 values

    public partial class Yekiler : Form
    {
    Utils Utility = new Utils();
    Form1 anaform = new Form1();

    public Yekiler()
    {
        InitializeComponent();
    }

    public void Yekiler_Load(object sender, EventArgs e)
    {
        anaform = Application.OpenForms["Form1"] as Form1;


        MessageBox.Show(anaform.GridOgrenci.ColumnCount.ToString()); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜