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



如何在datagridview控件中增加、删除和修改记录(用程序代码的方法)


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


如何在datagridview控件中增加、删除和修改记录(用程序代码的方法)
发表于:2007-06-05 08:50:48 楼主
即在程序运行中,手动修改datagridview中的某个数或内容,点击某个按钮实现对数据库的更新!
新手上路,望高手指点,万分感谢!!!
发表于:2007-06-05 08:53:091楼 得分:0
winform   or   webform?

给c#的代码行么?
发表于:2007-06-05 08:55:592楼 得分:0
给datagrid加一个linkbutton或者button   cloumn
页面中:
<asp:datagrid   id= "datagrid1 "   style= "z-index:   105;   left:   16px;   position:   absolute;   top:   104px "
runat= "server "   width= "994px "   height= "294px "   datakeyfield= "id "   allowpaging= "true "   autogeneratecolumns= "false "   allowsorting= "true "   backcolor= "skyblue "
forecolor= "black "   bordercolor= "gray "   borderwidth= "3px "   cellspacing= "1 "   oneditcommand= "operation_edit "   ondeletecommand= "operation_delete ">
程序中:
public   void   operation_edit(object   sender,datagridcommandeventargs   e)         //打开超级连接,传个值id到弹出窗口
{
string   sitemindexoperation_no=datagrid1.datakeys[e.item.itemindex].tostring();
string   urlx= "/quesmana/details.aspx?itemindexoperation_no= "+sitemindexoperation_no;
string   url   = " <script   language= 'javascript '> window.open( ' "+urlx+ " ', '详细信息 ',   'width=680,height=500,status=no,resizable=yes,top=70,left=150 ') </script> ";  
response.write(url);                                                   //打开详细信息
}
public   void   operation_delete(object   sender,datagridcommandeventargs   e)     //删除记录
{

string   id=datagrid1.datakeys[e.item.itemindex].tostring();
string   delsql= "delete   from   quessolve   where   quesid= "+id+ "   delete   from   quesinfo   where   id= "+id+ " ";
try
{
db.command(delsql).EXECutenonquery();
db.close();
}
catch(system.exception   ex)
{
button2.visible=false;
button3.visible=false;
button4.visible=false;
button5.visible=false;
label1.visible=false;
label2.visible=false;
textbox1.visible=false;
dropdownlist1.visible=false;
button1.visible=false;
button6.visible=false;
datagrid1.visible=false;
response.write( "数据库未能打开,请检查数据库连接.详细信息: "+ex);
}
display();
//string   javascript= " <script   language= 'javascript '> window.location.reload() </script> ";
//response.write(javascript);                   //刷新当前页面

}
发表于:2007-06-05 08:56:093楼 得分:0
用vb.net的
发表于:2007-06-05 09:05:534楼 得分:0
private   g_savestate   as   integer   =   -1

  private   sub   增加_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   btn_add.click
                g_savestate   =   0
        end   sub
    private   sub   修改_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   btn_amend.click
                    g_savestate   =   1
        end   sub
  private   sub   btn_save_click(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   btn_save.click
                dim   da   as   datarow
                try
                        if   g_savestate   =   0   then
             
                                '增加
                                da   =   table.newrow()
                                da(0)   =   txtbox1.text
                                da(1)   =   txtbox2.text
                                table.rows.add(da)

                                msgbox( "保存完毕 ",   msgboxstyle.okonly,   "确认 ")

                                '修改
                        elseif   g_savestate   =   1   then
                                dr(0)(0)   =   txtbox1.text
                                dr(0)(1)   =   txtbox2.text
                                msgbox( "修改完毕 ",   msgboxstyle.okonly,   "确认 ")
                       
                                              end   if


                        bc.updatetable(table)
                        me.datagrid.rows.clear()
                catch   ex   as   exception
                end   try
        end   sub

      public   function   updatetable(byval   tb   as   datatable)   as   boolean
                updatetable   =   true
                try
                        if   con.state   =   connectionstate.closed   then
                                con.open()
                        end   if
                        dap.update(tb)
                catch   ex   as   exception
                        updatetable   =   false
                        throw   ex
                finally
                        con.close()
                end   try
        end   function

public   function   selecttable(byval   tablename   as   string,   byval   tb   as   datatable)   as   boolean
                selecttable   =   true
                try
                        'database连接字符窜生成
                        con   =   new   sqlconnection(constr)
                        'database连接
                        if   con.state   =   connectionstate.closed   then
                                con.open()
                        end   if
                        'table   连接字符窜
                        dim   sqlstr   =   "select   *   from   "   &   tablename
                        dap   =   new   sqldataadapter(sqlstr,   con)
                        bld   =   new   sqlcommandbuilder(dap)
                        tb.clear()
                        tb.acceptchanges()
                        dap.fill(tb)
                catch   ex   as   exception
                        selecttable   =   false
                        throw   ex
                finally
                        con.close()
                end   try
        end   function
end   class


快速检索

最新资讯
热门点击