新手请教:代码编写直接在datagrid上增加和修改数据
在网上找了一些代码,可总是不全,编不出来,哪位可以指教以下,谢谢了
下面的代码是我加载数据时用的,把数据导入datagrid1之后,直接在表里修改,或者增加新行,应该怎么做?
'查询DataGrid
Private Sub conDataGrid(ByVal sql As String)
Dim conn As String = "data source = " + SystemInformation.ComputerName + " ;initial catalog = bank;integrated security = SSPI;persist security info = false;packet size = 4096 "
Dim connection As New SqlClient.SqlConnection(conn) '连接数据库
Dim dataset1 As New DataTable( "output ")
Dim adapter1 As New SqlClient.SqlDataAdapter(sql, conn)
connection.Open()
dataset1.Clear()
adapter1.Fill(dataset1)
adapter1.Dispose()
connection.Dispose()
dataset1.Dispose()
Me.DataGrid1.DataSource = dataset1
End Sub
[解决办法]
winform下的datagrid可以直接对数据进行编辑,readonly设为false即可,编辑结束后调用
SqlDataAdapter.Update(dataset)方法更新数据,使用SqlDataAdapter.Update方法应该注意的地方可以在msdn中找到实例