您的位置:程序门 -> .net技术 -> c#



无法实时刷新


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


无法实时刷新[已结贴,结贴人:sxlcom]
发表于:2007-01-18 16:23:00 楼主
private   void   toolstripbutton4_click(object   sender,   eventargs   e)
                {
                          sqlconnection   con   =   new   sqlconnection( "server=(local);database=hr;uid=sa;pwd=; ");

                                try
                                {

                                        con.open();
                                        sqlcommand   cmd   =   new   sqlcommand( "delete   from   tb_user   where   hr_user= ' "   +   m1   +   " '   and   hr_password= ' "   +   m2   + " ' ",   con);
                                        cmd.EXECutenonquery();
                                        messagebox.show( "记录删除成功! ");
                                     

                                 
                               
                               
                               
                                     
                                }
                                catch   (sqlexception   ae)
                                {
                                        messagebox.show(ae.message.tostring());
                                }
                                finally
                                {
                                        con.close();
                                }
                               
                }
执行成功.但datagridview1中数据不刷新,所删除的记录还在,但重新启动一下就不显示删除的那条记录了,请问如何在以上代码中进行实时刷新
发表于:2007-01-18 16:24:391楼 得分:0
应该重新绑定一下datagridview1的数据霸。
发表于:2007-01-18 16:26:002楼 得分:0
就是不知道应该重新绑定,请问题如何实现
发表于:2007-01-18 16:39:043楼 得分:1
你只是删除了数据库,没有刷新datagridview1,应该加上刷新的代码,将tb_user中的数据select一次,再填充导dataset中
发表于:2007-01-18 16:41:114楼 得分:1
你datagrid第一次绑定数据怎么绑定   再绑一次了
发表于:2007-01-18 16:43:135楼 得分:0
应该重新绑定一下datagridview1的数据
发表于:2007-01-18 16:53:276楼 得分:0
我只想在一个事件中完成这个动作
发表于:2007-01-18 16:58:507楼 得分:1
重新绑定一下datagridview1的数据
发表于:2007-01-18 17:03:098楼 得分:1
重新绑定一次数据
发表于:2007-01-18 17:05:589楼 得分:1
调用你绑定gridview的方法,再绑定一次.
也就是删除后,再绑定数据.这样就刷新了
发表于:2007-01-18 17:18:2110楼 得分:10
重新绑定...

private   void   databind()
{
        sqlconnection   con=new   sqlconnection( "server=.;database=student;uid=sa;pwd=0421 ");
        sqldataadapter   sda=new   sqldataadapter( "select   *   from   studentdetails ",con);
        dataset   ds=new   dataset();
        sda.fill(ds, "student ");
        this.datagridview.datasource=ds.tables[ "studnet "];
}

//删除操作
private   void   toolstripbutton4_click(object   sender,   eventargs   e)
                {
                          sqlconnection   con   =   new   sqlconnection( "server=(local);database=hr;uid=sa;pwd=; ");
                                try
                                {
                                      //执行删除                                    
                                }
                                catch   (sqlexception   ae)
                                {
                                        messagebox.show(ae.message.tostring());
                                }
                                finally
                                {
                                        con.close();
                                }
                            //重新绑定
                            databind();                      
                }
发表于:2007-01-18 17:36:1811楼 得分:0
楼上说的对,你在数据库中删除怎么可能直接反映到你的datagradview中呢!需要重新绑定,以取得更新的数据。
发表于:2007-01-18 17:36:4812楼 得分:0
我是直接databaseset.clear();   再重新绑定的
发表于:2007-01-18 17:46:1213楼 得分:0
to:
我是直接databaseset.clear();   再重新绑定的


代码贴出来看看..
发表于:2007-01-19 11:07:1914楼 得分:0
private   void   toolstripbutton4_click(object   sender,   eventargs   e)
                {
            sqlconnection   con   =   new   sqlconnection( "server=(local);database=hr;uid=sa;pwd=; ");

                                try
                                {

                                        con.open();
                                        sqlcommand   cmd   =   new   sqlcommand( "delete   from   tb_user   where   hr_user= ' "   +   m1   +   " '   and   hr_password= ' "   +   m2   + " ' ",   con);

                                        cmd.EXECutenonquery();
                                     
                                               
                                        messagebox.show( "记录删除成功! ");
                                     

                }
                catch   (sqlexception   ae)
                {
                        messagebox.show(ae.message.tostring());
                }
                finally
                {
                        con.close();
                }

                databind();

                }


    private   void   databind()
                {
                        sqlconnection   con   =   new   sqlconnection( "server=(local);database=hr;uid=sa;pwd=; ");
                        try
                        {
                                con.open();
                                sqldataadapter   cmd   =   new   sqldataadapter( "select   *   from   hr_user   ",   con);
                                dataset   ds   =   new   dataset();
                                cmd.fill(ds,   "hr_user ");
                                this.datagridview1.datasource   =   ds.tables[ "hr_user "];
                     
                        }
                        catch   (exception   ex)
                        {
                                messagebox.show(ex.message.tostring());

                        }
                        finally
                        {
                                con.close();
                        }


                }

提示对象'hr_user'无效


发表于:2007-01-19 12:00:0615楼 得分:0
顶一下,请帮忙解决
发表于:2007-01-19 12:30:3316楼 得分:5
private   void   databind()
                {
                        sqlconnection   con   =   new   sqlconnection( "server=(local);database=hr;uid=sa;pwd=; ");
                        try
                        {
                               
                                sqldataadapter   cmd   =   new   sqldataadapter( "select   *   from   hr_user   ",   con);
                                dataset   ds   =   new   dataset();
                                cmd.fill(ds,   "hr_user ");
                                this.datagridview1.datasource   =   ds.tables[ "hr_user "];
                     
                        }
                        catch   (exception   ex)
                        {
                                messagebox.show(ex.message.tostring());

                        }
                       


                }

sqldataadapter不需要打开connection的连接的,可以自动打开和关闭,但是这应该不是主要问题
应该没事的阿,连接字符串和表名与你的都一致的阿
发表于:2007-01-19 12:30:3717楼 得分:0
搞定了,谢谢大家的帮忙


快速检索

最新资讯
热门点击