| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1525 人关注过本帖
标题:各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
取消只看楼主 加入收藏
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
结帖率:100%
收藏
已结贴  问题点数:0 回复次数:8 
各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
Private Sub 数据导入ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 数据导入ToolStripMenuItem.Click
        Dim dresult As DialogResult = Me.openFileDialog1.ShowDialog()
        If dresult = System.Windows.Forms.DialogResult.OK Then
            Dim filepath As String = openFileDialog1.FileName
            If filepath = "" Then
                MessageBox.Show("请选择要导入的Excel文档!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            Else
                Dim ds As New DataSet()
                '取得数据集
                '调用下面的函数
                ds = xsldata(filepath)
                'dataGridView2.DataSource = ds.Tables[0];
                Dim errorcount As Integer = 0 '记录错误信息条数
                Dim updatecount As Integer = 0 '更新信息条数
                Dim insertcount As Integer = 0 '记录插入成功条数
                Dim mess As String = ""
                Dim i As Integer
                For i = 0 To (ds.Tables(0).Rows.Count) - 1
                    Dim mc As String = ds.Tables(0).Rows(i).Item(0).ToString  '读取每一行的第一列数据
                    Dim kbc As Double = CType(ds.Tables(0).Rows(i).Item(1), Double)
                    Dim hsc As Double = CType(ds.Tables(0).Rows(i).Item(2), Double)
                    Dim shtc As Double = CType(ds.Tables(0).Rows(i).Item(3), Double)
                    Dim ysc As Double = CType(ds.Tables(0).Rows(i).Item(4), Double)
                    Dim djc As Double = CType(ds.Tables(0).Rows(i).Item(5), Double)
                    Dim stc As Double = CType(ds.Tables(0).Rows(i).Item(6), Double)
                    Dim dzc As Double = CType(ds.Tables(0).Rows(i).Item(7), Double)
                    Dim hhc As Double = CType(ds.Tables(0).Rows(i).Item(8), Double)
                    Dim dhc As Double = CType(ds.Tables(0).Rows(i).Item(9), Double)
                    Dim zjc As Double = CType(ds.Tables(0).Rows(i).Item(10), Double)
                    Dim yx As Double = CType(ds.Tables(0).Rows(i).Item(11), Double)
                    Dim bgzl As Double = CType(ds.Tables(0).Rows(i).Item(12), Double)
                    Dim gjzs As Double = CType(ds.Tables(0).Rows(i).Item(13), Double)
                    Dim kxb As Double = CType(ds.Tables(0).Rows(i).Item(14).ToString, Double)
                    Dim yxzs As Double = CType(ds.Tables(0).Rows(i).Item(15).ToString, Double)

                    Dim Str As String = "insert into zuankong(名称,空白层,含水层,砂土层,岩石层,堆积层,死土层,地质层,火化层,地核层,中间层,岩性,比贯入阻力,标贯击数,孔隙比,液性指数) Values ('" + mc + "','" & Trim(kbc) & "','" & Trim(hsc) & "','" & Trim(shtc) & "','" & Trim(ysc) & "','" & Trim(djc) & "','" & Trim(stc) & "','" & Trim(dzc) & "','" & Trim(hhc) & "','" & Trim(dhc) & "','" & Trim(zjc) & "','" & Trim(yx) & "','" & Trim(bgzl) & "','" & Trim(gjzs) & "','" & Trim(kxb) & "','" & Trim(yxzs) & "') "
                    Dim connection As OleDb.OleDbConnection = New OleDb.OleDbConnection
                    connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & System.Windows.Forms.Application.StartupPath & "\zkshujuku.mdb;"
                    Dim shujubiaoadapter As New OleDb.OleDbDataAdapter
                    shujubiaoadapter.SelectCommand = New OleDb.OleDbCommand(Str, connection)
                    Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(shujubiaoadapter)
                    shujubiaoadapter. = Str
                    connection.Open()
                    shujubiaoadapter.Fill(ZkshujukuDataSet, "zuankong")
                    DataGridView1.DataSource = ZkshujukuDataSet.Tables("zuankong")
                    Dim MyTable As New System.Data.DataTable()
                    MyTable.Clear()
                    shujubiaoadapter.Fill(MyTable)
                    DataGridView1.DataSource = MyTable
                Next
                   End If
        End If
    End Sub

    Private Function xsldata(ByVal filepath As String) As DataSet

        Dim strCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'"

        Dim Conn As New System.Data.OleDb.OleDbConnection(strCon)

        Dim strCom As String = "SELECT * FROM [Sheet1$]"

        Conn.Open()

        Dim myCommand As New System.Data.OleDb.OleDbDataAdapter(strCom, Conn)

        Dim ds As New DataSet()

        myCommand.Fill(ds, "[Sheet1$]")
        Conn.Close()

        Return ds
    End Function 'xsldata
上面可以实现Excel导入到Access功能,但是导入成功后DataGridView控件上就变成空白了,不能显示数据表?本来DataGridView上还显示原来的数据表,插入这些数据后就什么不显示了!
最近发帖频繁 没有分了 不知道这个能不能增加悬赏  如果我下次有分了,一定补上  呵呵呵
搜索更多相关主题的帖子: Excel 大哥 
2011-07-06 15:13
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
还有为什么数据是成倍的增加呢?导入10个记录 增加了20个  看来我的确很菜  各位高手不要烦 为我排忧解难  谢谢
2011-07-06 16:40
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
2011-07-06 18:40
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 3楼 不说也罢
已经发了,在楼上,谢谢!
2011-07-06 18:40
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 6楼 不说也罢
谢谢你啊  你还是帮我解决第二个问题吧,增加记录为何都是成倍的,比如12条记录,在数据库里就变成了24条,每条重复一次,至于你说的我不是太懂,我想最后DataGridView显示的是更新后的数据库里的数据!
2011-07-06 21:06
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 8楼 不说也罢
我也知道直接要代码不好,我不知道你要我感觉什么 我的悟性比较差,我看这些代码看半天也没看出个所以然 我以前一点编程基础也没有 我最后也不是有DataGridView1.DataSource = MyTable吗 为什么不显示呢  为什么会多增加一倍的重复记录  我一点头绪都没有  我这几天天天看代码 头都大了 老板(导师)还催着要,他自己不懂编程,还以为编程很简单,每次都怀疑我偷懒!O__O"…
2011-07-06 22:50
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 8楼 不说也罢
  我现在找到症结了 谢谢你啊  下面是我修改后的代码
 Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection
                    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & System.Windows.Forms.Application.StartupPath & "\zkshujuku.mdb;"
                    Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
                    conn.Open() '打开连接
                    cmd.Connection = conn
                    Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(Str, conn)
                    da.Fill(ZkshujukuDataSet, "zuankong")
                    DataGridView1.DataSource = ZkshujukuDataSet.Tables(0) '更换datagridview1的数据源
                    DataGridView1.Refresh()
但还有一个问题 就是dataGridView上的记录还是不变 显示的还是原来的 BindingNavigator控件上的记录数当然也保持不变 怎么做才能增加数据记录后控件上的记录数也跟着增加呢?

[ 本帖最后由 zhangzhen 于 2011-7-7 21:36 编辑 ]
2011-07-07 21:28
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回答一下吧 我快急死了
2011-07-08 16:08
zhangzhen
Rank: 2
等 级:论坛游民
帖 子:92
专家分:27
注 册:2011-3-4
收藏
得分:0 
回复 12楼 不说也罢
非常感谢 问题解决 终于解决了一个问题
2011-07-08 20:00
快速回复:各位大哥大姐求教一下Excel导入到了Access但datagridview上不能显示?
数据加载中...
 
   



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

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