[VB.NET]如何在DataGridView控件中增加、删除和修改记录用程序代码的方法


如何在DataGridView控件中增加删除修改记录(用程序代码方法
即在程序运行中,手动修改DataGridView中的某个数或内容,点击某个按钮实现对数据库的更新!
新手上路,望高手指点,万分感谢!!!
__________________________________________________________________________
winform or webform?

给C#的代码行么?
__________________________________________________________________________
给DATAGRID加一个LINKBUTTON或者BUTTON CLOUMN
页面中:
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 = " "; 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= " "; //Response.Write(JavaScript); //刷新当前页面 } __________________________________________________________________________ 用vb.net的 __________________________________________________________________________ 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( "修改完毕 ","确认 ") 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 __________________________________________________________________________

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...