| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2466 人关注过本帖
标题:datagridview 重设数据源问题
取消只看楼主 加入收藏
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:6 
datagridview 重设数据源问题
datagridview 重设数据源后,原来数据源中有的列无法移除!!
试了一下
 DataGridView1.DataSource = Nothing
        Dim k As Integer = DataGridView1.ColumnCount - 1
        For j As Integer = 0 To k
            DataGridView1.Columns.Clear()
            DataGridView1.Columns.Remove(j)
        Next
        DataGridView1.Refresh()
仍不行,不要和我说重载表单
搜索更多相关主题的帖子: 数据源 datagridview 重设 
2010-04-18 13:45
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

是动态绑定的,我刚从VB6.0转过来,为了熟悉数据库,做了这样一个小程序,界面如上
单击“连接数据库”则显示数据所含表
单击“打开并绑定表”则显示包含字段名称并绑定到datagridview1
可以通过包含字段查看,该字段的类型!
现在其它功能都成了,只是更改表后,datagridview1数据不同步
Public ds As New Data.DataSet()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click
        Dim dbaname As String = txtdba.Text '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim tabname As String = cmbtable.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connStr)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
        Dim sql As String = "select * from " & tabname
        conn.Open()
        cmd.Connection = conn
        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)

        ds.Clear()
        da.Fill(ds, "pylj")
        DataGridView1.DataSource = Nothing
        Dim k As Integer = DataGridView1.ColumnCount - 1
        For j As Integer = 0 To k
            DataGridView1.Columns.Clear()
            DataGridView1.Columns.Remove(DataGridView1.SelectedColumns(j))
        Next
        DataGridView1.Refresh()
        MsgBox("d")
        DataGridView1.DataSource = ds.Tables(0)

        Dim n As Integer = ds.Tables(0).Columns.Count - 1
        Dim y As Integer = cmbfiled.Items.Count - 1
        For x As Integer = 0 To y

            cmbfiled.Items.Clear()
        Next
        cmbfiled.Refresh()

        For i As Integer = 0 To n
            cmbfiled.Items.Add(ds.Tables(0).Columns(i).ColumnName)

        Next

        cmbfiled.SelectedIndex = 0
    End Sub
这是打开表并绑定的代码,为了方便,把ds数据集设成全局变量
请高手指点

[ 本帖最后由 shenhaoli 于 2010-4-18 18:19 编辑 ]
2010-04-18 18:17
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
回复 楼主 shenhaoli
我把我的项目传上来,frm_data
运行时:数据库操作——》access
练习.rar (430.28 KB)
2010-04-18 18:33
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
是的,然后单击“打开表并绑定”datagridview1 同步有问题

[ 本帖最后由 shenhaoli 于 2010-4-18 18:53 编辑 ]
2010-04-18 18:52
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
图1先打开cyc表,图2是打开pylj表,datagridview1里仍有cyc表里的列
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2010-04-18 19:07
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
谢谢热心帮助
问题出在Public ds As New Data.DataSet() 上了,是吗?
现在cmbfiled_SelectedIndexChanged,显示该字段的数据类型,现在需要重新打开数据表了,这种方法我实现了
Public Class frm_data
    'Private ds As New Data.DataSet()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click
        Dim ds As New Data.DataSet()
        Dim dbaname As String = txtdba.Text '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim tabname As String = cmbtable.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connStr)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
        Dim sql As String = "select * from " & tabname
        conn.Open()
        cmd.Connection = conn
        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)

        ds.Clear()
        da.Fill(ds, "pylj")
        DataGridView1.DataSource = Nothing
        Dim k As Integer = DataGridView1.ColumnCount - 1
        'For j As Integer = 0 To k
        'DataGridView1.Columns.Clear()
        ' DataGridView1.Columns.Remove(DataGridView1.SelectedColumns(j))
        'Next
        DataGridView1.Refresh()
        MsgBox("d")
        DataGridView1.DataSource = ds.Tables(0)
        DataGridView1.Refresh()
        Dim n As Integer = ds.Tables(0).Columns.Count - 1
        Dim y As Integer = cmbfiled.Items.Count - 1
        For x As Integer = 0 To y

            cmbfiled.Items.Clear()
        Next
        cmbfiled.Refresh()

        For i As Integer = 0 To n
            cmbfiled.Items.Add(ds.Tables(0).Columns(i).ColumnName)

        Next

        cmbfiled.SelectedIndex = 0
    End Sub


    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        MsgBox(DataGridView1.CurrentCell.Value)
    End Sub

    Private Sub frm_data_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtdba.Text = "\data\py.mdb"
    End Sub

    Private Sub butconn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butconn.Click
        cmbtable.Items.Clear()
        For i As Int16 = 0 To GetAllTableName.Count - 1
            cmbtable.Items.Add(GetAllTableName.Item(i).ToString)
            'MsgBox(GetAllTableName.Item(i).ToString)
        Next

        cmbtable.SelectedIndex = 0







    End Sub
   

    Private Sub cmbfiled_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbfiled.SelectedIndexChanged
        Dim i As Integer = cmbfiled.SelectedIndex
        Dim ds As New Data.DataSet()
        Dim dbaname As String = txtdba.Text '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim tabname As String = cmbtable.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connStr)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
        Dim sql As String = "select * from " & tabname
        conn.Open()
        cmd.Connection = conn
        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)

        ds.Clear()
        da.Fill(ds, "pylj")
        txtfieldtype.Text = ds.Tables(0).Columns(i).DataType.Name
    End Sub

    Function GetAllTableName() As System.Collections.ArrayList
        Dim t As New System.Collections.ArrayList()
        Dim Rs As New ADODB.Recordset()
        Dim dbaname As String = txtdba.Text  '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim conn As New ADODB.Connection
        conn.Open(connStr)
        Rs = conn.OpenSchema(ADODB.SchemaEnum.adSchemaTables)
        Do While Not rs.EOF
            If rs("TABLE_TYPE").Value = "TABLE" Then
                t.Add(rs("TABLE_NAME").Value)
            End If
            rs.MoveNext()
            If rs.EOF Then Exit Do
        Loop
        Rs.Close()
        Rs = Nothing
        Return t
    End Function

End Class
现在我想把ds 定义成全局的,这样一句
txtfieldtype.Text = ds.Tables(0).Columns(i).DataType.Name
就可以
--------
现在我发现问题是ds里面数据没有清除,这个应该怎么办??
2010-04-18 20:53
shenhaoli
Rank: 1
等 级:新手上路
帖 子:24
专家分:2
注 册:2010-4-15
收藏
得分:0 
感谢“不说也罢”,问题成功解决,现把改正后代码同大家一起分享
程序代码:
Imports System.Data.SqlClient
Imports System.Data
Public Class frm_data
    Private ds As New Data.DataSet()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click

        Dim dbaname As String = txtdba.Text '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim tabname As String = cmbtable.Text
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(connStr)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand
        Dim sql As String = "select * from " & tabname
        conn.Open() '打开连接
        cmd.Connection = conn
        Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sql, conn)
        ds.Tables.Clear()
        da.Fill(ds, "pylj")
        DataGridView1.DataSource = ds.Tables(0) '更换datagridview1的数据源
        'DataGridView1.Refresh() '这句不要datagridview1也可更新
        '--------------
        '获取表所含字段并更新至cmbfiled
        Dim n As Integer = ds.Tables(0).Columns.Count - 1
        cmbfiled.Items.Clear()
        For i As Integer = 0 To n
            cmbfiled.Items.Add(ds.Tables(0).Columns(i).ColumnName)
        Next
        cmbfiled.SelectedIndex = 0
    End Sub
    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        MsgBox(DataGridView1.CurrentCell.Value)
    End Sub
    Private Sub frm_data_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtdba.Text = "\data\py.mdb"
    End Sub
    Private Sub butconn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butconn.Click
        cmbtable.Items.Clear()
        '显示数据所含表
        For i As Int16 = 0 To GetAllTableName.Count - 1
            cmbtable.Items.Add(GetAllTableName.Item(i).ToString)
        Next
        cmbtable.SelectedIndex = 0
    End Sub
    Private Sub cmbfiled_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbfiled.SelectedIndexChanged
        Dim i As Integer = cmbfiled.SelectedIndex
        '显示所选字段数据类型
        txtfieldtype.Text = ds.Tables(0).Columns(i).DataType.Name
    End Sub

    Function GetAllTableName() As System.Collections.ArrayList
        Dim t As New System.Collections.ArrayList()
        Dim Rs As New ADODB.Recordset()
        Dim dbaname As String = txtdba.Text  '指定数据库文件路径及名称
        Dim connStr As String = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & CurDir() & dbaname
        Dim conn As New ADODB.Connection
        conn.Open(connStr)
        Rs = conn.OpenSchema(ADODB.SchemaEnum.adSchemaTables)
        Do While Not Rs.EOF
            If Rs("TABLE_TYPE").Value = "TABLE" Then
                t.Add(Rs("TABLE_NAME").Value)
            End If
            Rs.MoveNext()
            If Rs.EOF Then Exit Do
        Loop
        Rs.Close()
        Rs = Nothing
        Return t
    End Function

End Class


2010-04-18 21:13
快速回复:datagridview 重设数据源问题
数据加载中...
 
   



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

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