标题:还有我编辑了 datagridview1 的列,可是加载数据时,datagridview1 却自动添 ...
只看楼主
worte
Rank: 1
等 级:新手上路
威 望:2
帖 子:4
专家分:0
注 册:2014-5-23
结帖率:0
已结贴  问题点数:20 回复次数:3 
还有我编辑了 datagridview1 的列,可是加载数据时,datagridview1 却自动添加了列,我编辑的列却是空的,请问怎么加载到自己的列上去啊。
我编辑了datagridview1的列,可是加载数据时,datagridview1却自动添加了列,我编辑的列却是空的,请问怎么加载到自己的列上去啊。

我编辑的datagridview1里面有包含datagridviewcombobox和datagridviewbutton的例, 还有数据怎么再保存进去数据库?

就是要实现:把数据加载到特定格式的datagridview里,修改完后再保存进数据库.

新手学习中,先谢谢了
搜索更多相关主题的帖子: 数据库 
2014-05-23 15:51
jianjunfeng
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:42
专家分:166
注 册:2009-3-13
得分:20 
有附件?
2014-05-30 11:51
worte
Rank: 1
等 级:新手上路
威 望:2
帖 子:4
专家分:0
注 册:2014-5-23
得分:0 
我用添加Datagridviewcomboboxcolumn列的代码如下:
程序代码:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            mycon.ConnectionString = "Server=.;Database=abcd;User ID=sa;Password=1234"
            sqltxt = "select * from Products"
            If mycon.State = ConnectionState.Closed Then mycon.Open()
            mycmd = New SqlClient.SqlCommand(sqltxt, mycon)
            myda = New SqlClient.SqlDataAdapter(mycmd)
            myda.Fill(ds, "Categories")
            mytab = ds.Tables("Categories")
            DataGridView1.DataSource = mytab
            Dim abc() As String = {"", "", "", "绿", "", "", ""}
            Dim List As New DataGridViewComboBoxColumn()
            List.Items.AddRange(abc)
            List.DisplayIndex = 1
            List.HeaderText = "CategoryName"
            DataGridView1.Columns.Add(List)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

效果是这样:
如图一:
第二列及第三列没办法合在一起
要怎么写才能合在一起呢?

我要的效果是如图二,图二是我用Combobox做成的,但上下左右翻页后,位置不懂得控制

(另:这怎么贴不了图进来?
2014-05-31 10:42
jianjunfeng
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:42
专家分:166
注 册:2009-3-13
得分:0 
回复 3 楼 worte
Imports System.Data.SqlClient
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim mycon As New SqlConnection("data source=192.168.1.85;initial catalog=prod;Persist Security Info=True;user id=sa;password=sa;")

            Dim sqltxt As String = "select * from Products"
            If mycon.State = ConnectionState.Closed Then mycon.Open()
            Dim ds As New DataSet
            Dim da As SqlDataAdapter
            da = New SqlDataAdapter(sqltxt, mycon)
            da.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
            mycon.Close()
            mycon = Nothing
            
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
        With Me.DataGridView1
            .Controls.Clear()
            If .Rows.Count > 0 Then
                Dim GH As Integer = .CurrentCell.ColumnIndex
                Dim GF As Integer = .CurrentCell.RowIndex
                If GH = 1 Then
                    .Controls.Add() ';//dataGr
                     = .Item(1, GF).Value.ToString.Trim
                     = .GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Width ';//获取单元格高并设置为btn的宽
                    ' = .GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Height ';//获取单元格高并设置为btn的高
                     = New System.Drawing.Point(((.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Right) - ()), .GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Y) ';//设置btn显示位置

                End If
            End If
        End With
    End Sub
    Private Sub DataGridView1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseWheel
        Me.DataGridView1.Controls.Clear()
    End Sub

    Private Sub DataGridView1_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll
        Me.DataGridView1.Controls.Clear()
    End Sub
End Class



ComboBox1 加上数据

我是这样处理的

[ 本帖最后由 jianjunfeng 于 2014-6-4 19:39 编辑 ]
2014-06-04 19:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-431952-1-1.html




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

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