| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 926 人关注过本帖
标题:datagrid 怎么不能实时更新呢??
只看楼主 加入收藏
hytf
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-11
收藏
 问题点数:0 回复次数:6 
datagrid 怎么不能实时更新呢??

Option Explicit

Public g_Conn As Connection '当前活动连接
Public g_username As String '当前登陆用户
Public g_showdate As Date '登陆时间

'系统由此启动
Sub main()
Dim msg As String
On Error Resume Next
msg = connecttodatabase("sdcl.mdb")
If msg <> "" Then
MsgBox "连接数据库失败" & msg, 16, "登陆"
End
End If
frmmain.Show vbModal
End Sub

'连接到数据库
Public Function connecttodatabase(strFileName As String) As String
On Error GoTo err_conn
Set g_Conn = New Connection
With g_Conn
.CursorLocation = adUseClient
.CommandTimeout = 10
'连接到数据库
.ConnectionString = "provider=microsoft.jet.oledb.4.0;password='';" & _
"data source=" & App.Path & "\" & strFileName
.Open
End With
connecttodatabase = ""
Exit Function

err_conn:
connecttodatabase = Err.Description
End Function

'替换单引号
Function realstring(strsrc As String) As String
strsrc = Replace(strsrc, "'", "''")
End Function

‘’‘’以下为frmmain内容
Private Sub CmdInput_Click()
On Error Resume Next
Dim strsql As String
Adodc1.LockType = adLockBatchOptimistic
Adodc1.ConnectionString = g_Conn.ConnectionString

strsql = "Select 站号,后尺下丝,后尺上丝,前尺下丝,前尺上丝,后尺黑面,后尺红面,前尺黑面,前尺红面 From sdcl "
Adodc1.RecordSource = strsql
Adodc1.Refresh

DataGrid1.ReBind
'导出到Excel文件中
Dim rstCount As Long '记录行数
Dim rstField As Long '记录列
rstCount = Record.RecordCount
rstField = Record.Fields.Count
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
'Dim ArrTemp() As String
xlApp.Visible = False
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(App.Path & "\three.XLS")
Set xlSheet = xlBook.Worksheets(1)
xlBook.Close '---------不按内容变化关闭
xlApp.Quit '--------关闭创建的文件

Set xlBook = Nothing '------释放对象中的值
Set xlApp = Nothing
Set xlSheet = Nothing

If Not (Record Is Nothing) Then
Record.Close
Set Record = Nothing
End If
End Sub
Private Sub Form_Load()
With DataGrid1
.Top = (frmthree.Height - .Height) / 8
.Left = (frmthree.Width - .Width) / 4
.Width = 10000
.Height = frmthree.Height / 2
.AllowAddNew = True
.AllowDelete = True
.HeadFont = "30"
.HeadFont.Bold = True
.Font.Size = "12"
.Font.Name = "宋体"
.Columns(0).Width = 800
.Columns(i).Alignment = dbgCenter
For i = 1 To 8
.Columns(i).Width = 1100
.Columns(i).NumberFormat = "#0.000"
.Columns(i).Alignment = dbgCenter
Next i
End With
End Sub

搜索更多相关主题的帖子: datagrid 实时 
2007-08-10 15:23
hytf
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-11
收藏
得分:0 
这么长时间怎么没人给改改呢????
2007-08-14 08:48
ouzhiguang
Rank: 1
来 自:湖南长沙
等 级:新手上路
威 望:1
帖 子:240
专家分:0
注 册:2007-5-18
收藏
得分:0 
你的程序里面datagrid。refesh
还有修改应该有datagrid.updata
我没上机测试过,不过凭经验是这样的。。。
2007-08-14 10:57
hytf
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-11
收藏
得分:0 
能给改改吗???他就是不能更新啊
yZZyv043.rar (31.34 KB) datagrid 怎么不能实时更新呢??


解压密码为:11
2007-08-15 16:25
qlong0728
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:272
专家分:0
注 册:2007-6-15
收藏
得分:0 
可能是因为很少人用你这种方法写程序啦吧

在VB里,一般人都是用到ADODB的
比如:
Public cn As ADODB.Connection
StrSQL = "Provider=SQLOLEDB.1;Password=" & SQLPassword & ";User ID=" & mem_SQLuser & ";Initial Catalog=" & mem_SQLInitialCatalog & ";Data Source=" & mem_SQLdatasource & ",1433;"
cn.Open StrSQL
dim rs_gyszl as ADODB.Recordset
Set rs_gyszl = New ADODB.Recordset
rs_gyszl.Open "select * from s_kh order by dh", cn, adOpenStatic, adLockOptimistic
Set Ddatagrid.DataSource = rs_gyszl

如果要刷新就简单多了

rs_gyszl.Requery 就可以了

直接用语言来赋数据源

程序人员写程序,又拿程序换酒钱。 奔驰宝马贵者趣,公交自行程序员。 不见满街漂亮妹,哪个归得程序员。
2007-08-15 17:42
hytf
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-11
收藏
得分:0 
哦,难怪没人恢复哪?
我现在刚刚自学,全凭自己摸索
也不知道哪个好哪个坏,
现在只要把一堆代码落起来能运行,并能达到目的就行了
还望大侠能指点一二
我这个程序应该怎么做?

Data Source=" & mem_SQLdatasource & ",1433;"

mem_SQLdatasource什么意思?
1433 什么意思?

[此贴子已经被作者于2007-8-16 15:02:29编辑过]

2007-08-16 14:56
qlong0728
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:272
专家分:0
注 册:2007-6-15
收藏
得分:0 
Data Source="数据库名"

1433 SQLserver端口

程序人员写程序,又拿程序换酒钱。 奔驰宝马贵者趣,公交自行程序员。 不见满街漂亮妹,哪个归得程序员。
2007-08-28 18:56
快速回复:datagrid 怎么不能实时更新呢??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015917 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved