Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

Im a newbie in C#, i have created a test windows form program.
I have 2 Forms, i need to pass the value of textbox from Form2 to Form1 DataGridView. All form are open (running). I encountered error using Application.Openforms.

Please see the error message and my code below. (sorry for my grammar).

Error Message on (Form1 f1 = Application.OpenForms["Form1"];)
===================================================================
Cannot Implicitly Convert Type 'System.Windows.Forms.Form' To 'gridviewsample1.Form1'.
An explicit conversion exists (are you missing a cast?)

FORM2 code
====================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace gridviewsample1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
Form1 f1 = Application.OpenForms["Form1"];

foreach (DataGridView row in f1.dataGridView1.SelectedRows)
{

row.Cells[1].Value = this.txtstring1.Text;
row.Cells[0].Value = this.txtstring2.Text;
}
}
}
}
Posted

Hi,

Try this:
C#
Form1 f1 = Application.OpenForms["Form1"] as Form1;

Hope this helps.
 
Share this answer
 
Hi
Go fot the get and set proprty, or declare a public static vairable in Form1 and call this in Form2. as - eg :- string str = Form1.variable,

If you directly want to assign to data gridview the can do like this -

Dtgrdview.rows[0].cells[0].value = Form1.variable;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900