[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( "修改完毕 ", 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
__________________________________________________________________________

你可能感兴趣的:([VB.NET论坛])