之后怎么在主窗体实现这个方法啊 实现的方法要把查到的数据显示在主窗体的datagridview上面

解决方案 »

  1.   

    可以通过Owner绑定两者的关系,具体如下
    父窗体form1子窗体form2
    form1打开form2:Form2 frm2 = new Form2();frm2.Owner=this;frm2.show();
    子窗体调父窗体方法:form2 Form1 frm1;frm1 = (Form1)
     this.Ownerfrm1.更新datagridview(参数是frm2查询的结果)
      

  2.   

    一旦你随意地进行循环依赖设计,很快,你的多个class、一大堆对象实例,就变成一团乱麻,纠结起来,也不能区分出哪些功能是服务那些功能是客户,作用也就降低了。
      

  3.   

    比如说你使用最为“傻瓜化”的TextBox控件,它可以让你扩展功能来检查按键是否合法(例如当按下Shift键的时候就进行某个字符串格式化,输入不合法字符的时候就禁用掉当前输入的字符),这就是通过它的KeyDown等事件来通知你,然后你就可以捕获它这个事件,读取这个事件返回的参数,读取当前键盘功能键状态,读取当前TextBox的Text属性,调用TextBox控件的方法或者重置它的Text属性。人家的TextBox控件不会要求你传送给它什么Form(Owner)参数的。因为人家根本不会想当然地去假设宿主是Form,宿主完全可以是任何一种容器控件,比如说你可以把TextBox拖到你自己的另外一个用户控件中。“子窗口”的设计技术是一样的。想当然地去随便设计当然可以,但是功能就弱爆了,无法放到稍微“大”一点的设计上去扩展应用。而遵循我们简单的设计原则,你也许只是多花了1分钟时间去多思考一下设计规范,但是不白学一次组件设计。
      

  4.   

    我知道你想干什么,就是弹出个查询窗口,查询完成后窗口关闭,并且主窗口执行查询并显示数据。正如sp1234所说,既然父窗体已经知道子窗体的属性并预先给子窗体传值,子窗体查询的返回值父窗体也就可以知道了,子窗体中没必要再知道父窗体中有什么属性和方法。首先假如form1是父窗体,form2是子窗体。
    form1打开form2之前,一定会先给form2传参数,这些参数可以在form2中定义成public型
    然后用form2.showdialog()的方式打开form2,这样在form2关闭之前,主窗体代码不再继续执行,而是等待form2关闭。
    form2关闭的时候,关闭按钮中写代码,保存查询选择内容
    然后form1继续执行showdialog之后的代码,你可以在这里获取form2的选择内容并查询
      

  5.   

    实现动态按钮的窗体界面
    private void button1_MouseMove(object sender, MouseEventArgs e)
            {
                button1.ImageAlign = ContentAlignment.MiddleLeft;//鼠标移动到button1时改变图片位置
            }
            private void button2_MouseMove(object sender, MouseEventArgs e)
            {
                button2.ImageAlign = ContentAlignment.MiddleLeft;//鼠标移动到button2时改变图片位置
            }
            private void button3_MouseMove(object sender, MouseEventArgs e)
            {
                button3.ImageAlign = ContentAlignment.MiddleLeft;//鼠标移动到button3时改变图片位置
            }
            private void button4_MouseMove(object sender, MouseEventArgs e)
            {
                button4.ImageAlign = ContentAlignment.MiddleLeft;//鼠标移动到button4时改变图片位置
            }
            private void button5_MouseMove(object sender, MouseEventArgs e)
            {
                button5.ImageAlign = ContentAlignment.MiddleLeft;//鼠标移动到button5时改变图片位置
            }
            private void button1_MouseLeave(object sender, EventArgs e)
            {
                button1.ImageAlign = ContentAlignment.MiddleCenter;//鼠标移动到button1时改变图片位置
            }
            private void button2_MouseLeave(object sender, EventArgs e)
            {
                button2.ImageAlign = ContentAlignment.MiddleCenter;//鼠标离开时改变button2图片位置
            }
            private void button3_MouseLeave(object sender, EventArgs e)
            {
                button3.ImageAlign = ContentAlignment.MiddleCenter;//鼠标离开时改变button3图片位置
            }
            private void button4_MouseLeave(object sender, EventArgs e)
            {
                button4.ImageAlign = ContentAlignment.MiddleCenter;//鼠标离开时改变button4图片位置
            }
            private void button5_MouseLeave(object sender, EventArgs e)
            {
                button5.ImageAlign = ContentAlignment.MiddleCenter;//鼠标离开时改变button5图片位置
            }
            private void button6_MouseMove(object sender, MouseEventArgs e)
            {
                button6.ImageAlign = ContentAlignment.MiddleLeft;//鼠标移动到button6时改变图片位置
            }
            private void button6_MouseLeave(object sender, EventArgs e)
            {
                button6.ImageAlign = ContentAlignment.MiddleCenter;//鼠标离开时改变button6图片位置
            }
      

  6.   

    this's ok!使用事件通知或者委托的方式来解决吧,虽然条条大路通罗马。
      

  7.   

    通过子窗体刷新父窗体
    void BabyWindow_UpdateDataGridView(object sender,EventArgs e)
            {
                if(Frm_Child.GlobalFlag == false)    //当单击删除按钮时
                {
                    if(ConnPubs.State == ConnectionState.Closed) //当数据库处于断开状态时
                    {
                        ConnPubs.Open();                //打开数据库的连接
                    }
                    string AfreshString = "delete tb_User where userID=" + Frm_Child.DeleteID.Trim();//定义一个删除数据的字符串
                    PersonalInformation = new SqlCommand(AfreshString,ConnPubs); //执行删除数据库字段
                    PersonalInformation.ExecuteNonQuery(); //执行SQL语句并返回受影响的行数
                    ConnPubs.Close();                     //关闭数据库
                    DisplayData();                          //显示数据库更新后的内容
                    MessageBox.Show("数据删除成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);//弹出删除数据成功的提示
                }
                else
                {
                    if(ConnPubs.State == ConnectionState.Closed) //当数据库处于关闭状态时
                    {
                        ConnPubs.Open();                        //打开数据库
                    }
                    string InsertString = "insert into tb_User values('" + Frm_Child.idContent + "','" + Frm_Child.nameContent + "','" + Frm_Child.phoneContent + "','" + Frm_Child.addressContent + "')";//定义一个插入数据的字符串变量
                    PersonalInformation = new SqlCommand(InsertString,ConnPubs);//执行插入数据库字段
                    PersonalInformation.ExecuteNonQuery();//执行SQL语句并返回受影响的行数
                    ConnPubs.Close();                    //关闭数据库
                    DisplayData();                         //显示更新后的数据
                    MessageBox.Show("数据添加成功!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);//弹出添加成功的提示信息
                }//节选CodeGo.net/