form1 中有textbox1,在textbox1回车后,弹出form2,双击form2中的dataGridView1中的一列,显示某一列的值在form1 的textbox1中。并关闭form2,求代码;dataGridView1中的值为: String sql2 = "select [序号],[医院名称],[服务项目],[报销类别],[申请时间],[报销时间],[项目金额],[报销标志],IIF([报销标志]='1','已报销','未报销') as 报销状态 from memoA order by [序号] desc "; //String sql2 = "select [userid] from userB"; DataSet ds = new DataSet(); ds = Cdatabase.GetDataFromDB(sql2); this.dataGridView1.DataSource = ds.Tables[0]; 请问如何实现,最好是有form1和form2的实现代码
public class Form1 : Form
{
private TextBox txtBox; public Form1()
{
txtBox = new TextBox();
txtBox.Location = new Point(0, 0);
txtBox.KeyPress += new KeyPressEventHandler(txtBox_KeyPress);
} void txtBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
Form2 frm2 = new Form2();
if (frm2.ShowDialog() == DialogResult.OK)
{
txtBox.Text = frm2.ReturnValue;
}
}
}
} public class Form2 : Form
{ public string ReturnValue
{
get;
set;
} public Form2()
{
} private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
ReturnValue = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Form2 f = new Form2();
f.ShowDialog(this);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Form1 f = (Form1)this.Owner;
f.Txt = this.dataGridView1.Rows[e.RowIndex].Cells[你的列名].Value.ToString();
this.Close();
}
}
Form1 f = (Form1)this.Owner; ---
f.Txt = this.dataGridView1.Rows[e.RowIndex].Cells[你的列名].Value.ToString();
第一句话就会报“无法将类型为“.Frmmain”的对象强制转换为类型“.Form1”。”
这样如何解决呢?