我是Vb新手现在想对DataGrid进行新增、修改、删除、保存数据的操作
想利用text、combobox这些控件传入数据来对DataGrid完成以上操作比如:在修改时,先在DataGrid中选定一行数据,然后在text,combobox中输入新的内容,点击保存按钮后,DataGrid就显示新的内容了。请教高手!
能否给出新增、修改、删除、保存这些操作详细的原代码?
因为我目前还很菜!谢谢了。
想利用text、combobox这些控件传入数据来对DataGrid完成以上操作比如:在修改时,先在DataGrid中选定一行数据,然后在text,combobox中输入新的内容,点击保存按钮后,DataGrid就显示新的内容了。请教高手!
能否给出新增、修改、删除、保存这些操作详细的原代码?
因为我目前还很菜!谢谢了。
解决方案 »
- 有写过地磅程序吗?帮看看代码!谢谢!
- MSFlexGrid或MSHFlexGrid的用法!
- 关于VB的WINSOCK,从客户端发送数据到服务端断点续传的问题,请诸位高手指点迷津!
- 怎样把自己定义的变量作为数据库查询的条件?
- 如何在Win98Sec系统中利用WMI读取网卡物理地址?
- 客户端怎样调用服务器端的组件(MTS)?
- 菜鸟问题,关于IF嵌套的问题
- sql2000数据库虚拟ip用户无法连接问题求教...
- 在SysTray里的PopupMenu问题
- 请问如何将dbgrid或true dbgrid某一行的网格记录打印出来?
- 我想知道怎么将我的程序同SQLServer2000打包到一起
- 网络计算机的异机备份?
在更新、添加方面我做的方法是做批量的更新和添加,比如在一个command下,用循环执行相应的sql语句'*************************************************************'
'* 模块名称:Module2 (Code)
'*************************************************************Option ExplicitPublic x As Long '记录flex的Row
Public y As Long '记录flex的Col'如果要留住输入的文本在单元格内,在txt1_LostFocus事件添加相应代码
'文本框移动到点击的单元 (flex_Click事件调用)
'***************************************************************************************
'* 过程名称:movetxt1
'* 功能: 文本框移动到点击的单元
'* 输入: txt(文本框对象), flex(网格对象)
'* 变量:
'**************************Sub movetxt1(txt As TextBox, flex As MSFlexGrid)
With flex
If .Col >= 0 And .Col <= 1 Then
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
End If
End With
End Sub'支持键盘的上下左右键 (txt1_KeyDown事件调用)
'***************************************************************************************
'* 过程名称:movetxt2
'* 功能: 支持键盘的上下左右键,并随之移动
'* 输入: txt(文本框对象), flex(网格对象),key(keyCode)
'* 变量: temp(临时记录flex的Row)
'**************************Sub movetxt2(txt As TextBox, flex As MSFlexGrid, key As Integer)
Dim temp As Long '临时记录flex的Row
With flex
Select Case key '向上移动
Case Is = 38
If .Row <= 1 And .Col > 0 Then
temp = .Row
.Row = 10
.Col = .Col - 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(temp, .Col + 1) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
Else
If .Row <= 1 Then
Exit Sub
End If
.Row = .Row - 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(.Row + 1, .Col) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
End If
Case Is = 40 '向下移动
If .Row >= 10 And .Col < 1 Then
temp = .Row
.Row = 1
.Col = .Col + 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(temp, .Col - 1) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
Else
If .Row >= 10 Then
Exit Sub
End If
.Row = .Row + 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(.Row - 1, .Col) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
End If
Case Is = 37 '向左移动
If .Col <= 0 And .Row > 1 Then
temp = .Col
.Col = 1
.Row = .Row - 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(.Row + 1, temp) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
Else
If .Col <= 0 Then
Exit Sub
End If
.Col = .Col - 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(.Row, .Col + 1) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
End If
Case Is = 39 '向右移动
If .Col >= 1 And .Row < 10 Then
temp = .Col
.Col = 0
.Row = .Row + 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(.Row - 1, temp) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
Else
If .Col >= 1 Then
Exit Sub
End If
.Col = .Col + 1
txt.Left = .Left + .CellLeft
txt.Top = .Top + .CellTop
txt.Width = .CellWidth
txt.Height = .CellHeight
.TextMatrix(.Row, .Col - 1) = txt.Text
txt.Text = .TextMatrix(.Row, .Col)
txt.Visible = True
txt.SetFocus
x = .Row
y = .Col
End If
Case Else
Exit Sub
End Select
End With
End Sub调用:
Private Sub flex_Click()
Call movetxt1(txt1, flex)
End SubPrivate Sub txt1_KeyDown(KeyCode As Integer, Shift As Integer)
Call movetxt2(txt1, msg1, KeyCode)
End SubPrivate Sub txt1_LostFocus()
txt1.Visible = False
msg1.TextMatrix(x, y) = txt1.Text
End Sub
你可以把DataGrid控件和文本框之类的控件绑定到同一数据源,这样它们的显示和更新就是同步的了。 你可以把我下面的代码拷进你的程序中去试一下(不过不能保证无错,因为我全是凭记忆手写的,可能会某些地方有错,实际情况请自行处理),并在你的窗体中加进一个文本框控件、二个按钮控件和一个DataGrid控件(两个按钮控件分别叫"存储" "取消"),并引用MS ActiveX data objects 2.7 library 在窗体的load事件中加入下面代码:
dim adoconn as new adodb.connecttion
dim adorec as new adodb.recordset
adoconn.connectionstring="这里输入具体的连接字串"
conn.cursorlocation = adUseclient'把游标设为本机,否则各绑定控件中将可能无法显示记录
adoconn.open
adoconn.begintrans '让连接以事务的方式开始一个新事务
dim StrSql as string
StrSql="这里输入你的具体的SQL语句"
adorec.open strsql,adoconn,adOpenKeyset, adLockBatchOptimistic'以立即键反应的打开方式和乐观批锁定的锁定方式打开记录集
set text1.datasource=adorec 'text1是文本框控件名,就是你要为其绑定数据源的文本框控件
text1.datafield=adorec.fields(0).name '注意:这里adorec.fields(0).name是你要将文本框绑定到其上的字段名,具体情况请具体替换。
set DataGrid1.datasource=adorec 'Datagrid1就是数据控件
这样在窗体载入后,你可以在DataGrid控件中不同的行和列中单击试试,看看文本框中的内容是否跟着变了。然后试着修改DataGrid中与文本框绑定字段对应的列的值,再把鼠标单击别处,看文本框中的内容是否也跟着修改了,然后再反过来修改文本框中的值,看对应的Datagrid控件中的单元格的值是否也变了.不过不要担心这一步中把数据库值修改了,它并不会反应到数据库,因为打开方式中已经设定打开方式和锁定类型了。 在"存储"命令按钮的单击事件中加入如下代码:
adorec.updatebatch '更新所有操作(到数据记录集),但还没有提交到数据库
adoconn.committrans '结束事务,提交到数据库,刚刚的修改才真正作用到数据库了
adoconn.begintrans '重新开始下一个事务
在"取消"命令按钮的单击事件中加入如下代码:
adorec.cancelBatch '取消所有更新操作
adoconn.committrans '结束事务
adoconn.begintrans '重新开始事务
楼主照我的意思去试一下,看是不是你想要的,我刚学VB,很多不懂,望各位大侠指教!!
但现在出现了大问题!!!!!!
我的datagrid和mshflexgrid控件均无法使用,一旦“保存”了程序或者点击“运行”后,这两个控件就会自动变成picturebox控件而且出现提示:
Component not correctly registered.和
Error loading 'C:\windows\temp\vb40E3.tmp'.A control could not be loaded due to load error.
我也试过下载了microsoft的补丁,也运行了vb安装光盘上tools文件夹下controls包中的注册程序。都没有用。
我们公司用的是正版的VB5.0和6.0,怎么还会有这种问题啊????
救命!!!!
另外对于这个问题,多半是对应的控件文件丢失了。
以上只是我的个人建议,不一定行得通,你可以参考别人的意见。
我刚学VB很多不懂,还望各位大侠指教!
但找不到我想要的datagrid和mshflexgrid,
有人知道怎么找到他们吗?要引用哪个组件啊?
谢谢了!
以上意见仅供参考!!