重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
DataGridView控件,放一个DataGridView1和Button1到窗体,下面是按钮下代码
站在用户的角度思考问题,与客户深入沟通,找到郁南网站设计与郁南网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册、雅安服务器托管、企业邮箱。业务覆盖郁南地区。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.DataGridView1.AllowUserToAddRows = False
DataGridView1.RowTemplate.Height = 200
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
For i = 1 To 3
Me.DataGridView1.Columns.Add("列" i.ToString, "列" i.ToString)
Me.DataGridView1.Rows.Add()
Next
Me.DataGridView1.Columns(0).Width = 100
Me.DataGridView1.Columns(1).Width = 500
Me.DataGridView1.Columns(0).Width = 300
End Sub
'自己设置相关需要的属性即可
步骤如下:
1. 先到C:\WINDOWS\system32目录下看看你的系统里是否已经有了MSADODC.ocx和MSDATGRD.ocx这两个文件(多半是没有的),要是有了就直接看第2步吧。
然后放到C:\WINDOWS\system32目录下。
2. 在VS 2005中注册MSADODC.ocx和MSDATGRD.ocx这两个控件。在VS 2005中点击"Tools" -- "Visual Studio 2005 Command Prompt",输入如下命令即可:
regsvr32 msadodc.ocx
regsvr32 msdatgrd.ocx
Private WithEvents NewTextBox As TextBox
'通过使用WithEvents关键字声明一个对象变量为新的命令按钮
Private Sub Command1_Click()
If NewTextBox Is Nothing Then
Set NewTextBox = Controls.Add("VB.TextBox", "cmdNew", Form1)
NewTextBox.Move 200, 200
NewTextBox.Width = Form1.Width - 450
NewTextBox.Height = Form1.Height - 1400
NewTextBox.Visible = True
End If
End Sub
Private Sub Command2_Click()
If NewTextBox Is Nothing Then
Exit Sub
Else
Controls.Remove NewTextBox
Set NewTextBox = Nothing
End If
End Sub
Dim tab1 As String
Dim DS As New DataSet
Dim SqlStr As String
Dim DTS_A As New DataGridTableStyle
Dim Col_A1, Col_A2, Col_A3 As New DataGridTextBoxColumn
Dim Col_bo As New DataGridBoolColumn
'DS.Tables("tab1").Columns.Add("check", System.Type.GetType("System.Boolean"))
'DS.Tables("tab1").Columns("check").DefaultValue = False
Dim Col_A() As DataGridColumnStyle = {Col_bo, Col_A1, Col_A2, Col_A3}
'主窗口
Private Sub fICitem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar1.Hide()
grdStyles("tab1")
SQLConn(conn)
Dim i As Integer
'Dim arr As New ArrayList
SqlStr = "SELECT ID, diqu, dname FROM a_diqu Order by ID"
If Not SQLFill(conn, DA, DS, SqlStr, "diqu") Then
MsgBox("服务器连接失败")
Else
Dim dr As DataRow
For Each dr In DS.Tables("diqu").Rows
'arr.Add(dr("diqu") "." dr("dname"))
cmbDiqu.Items.Add(dr("diqu") "." dr("dname"))
Next
End If
'cmbDiqu.DataSource = arr
cmbDiqu.SelectedIndex = -1
cmbDiqu.Text = ""
End Sub
'DataGrid格式
Private Sub grdStyles(ByVal tab As String)
Dim ii1 As Integer
Dim DTS_A1() As String = {"check", "fshortnumber", "fnumber", "fplanprice"}
Dim DTS_A2() As String = {"检查框", "货号", "上市月份", "单价"}
Dim DTS_A3() As Integer = {50, 120, 120, 120} '长度
Dim DTS_A4() As Integer = {2, 0, 2, 1} '对齐方式
DTS_A.MappingName = tab
DTS_A.GridColumnStyles.Clear()
DTS_A.AllowSorting = True
DTS_A.ReadOnly = True
DTS_A.BackColor = Color.DarkGray
DTS_A.AlternatingBackColor = Color.LightGray
For ii1 = 0 To DTS_A1.Length - 1
Col_A(ii1).MappingName = DTS_A1(ii1)
Col_A(ii1).HeaderText = DTS_A2(ii1)
Col_A(ii1).Width = DTS_A3(ii1)
Col_A(ii1).NullText = ""
Col_A(ii1).Alignment = DTS_A4(ii1)
Next
DTS_A.GridColumnStyles.AddRange(Col_A)
grdItem.TableStyles.Add(DTS_A)
grdItem.ReadOnly = True
End Sub
Private Sub fICitem_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
SQLClose(conn)
End Sub
'显示表单
Private Sub Show1()
grdStyles("tab1")
txtNum.Text = Trim(txtNum.Text)
grdItem.DataSource = Nothing
SQLStr = "select * from t_icitem_new"
SQLStr = " where fshortnumber like '" txtNum.Text "%'"
SQLStr = " order by fshortnumber"
SQLFill(conn, DA, DS, SQLStr, tab1)
grdItem.CaptionText = "货号资料"
grdItem.DataSource = DS.Tables(tab1)
ShowRow()
End Sub
'数据同步显示
Private Sub ShowRow()
Dim dr As DataRow
If DS.Tables(tab1).Rows.Count 0 Then
If grdItem.CurrentRowIndex DS.Tables(tab1).Rows.Count Then
grdItem.CaptionText = grdItem.CurrentRowIndex + 1 "/" DS.Tables(tab1).Rows.Count
dr = DS.Tables(tab1).Rows(grdItem.CurrentRowIndex)
txtNum.Text = dr.Item("fshortnumber")
txtPrice.Text = dr.Item("fplanprice")
cmbMonth.Text = dr.Item("fnumber")
Else
txtNum.Text = ""
txtPrice.Text = ""
cmbMonth.Text = ""
End If
Else
txtNum.Text = ""
txtPrice.Text = ""
cmbMonth.Text = ""
End If
End Sub
Private Sub grdItem_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdItem.CurrentCellChanged
ShowRow()
End Sub
'鼠标选定行
Private Sub grdItem_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdItem.CurrentCellChanged
ShowRow()
End Sub
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnSelect.Click, btnUpdate.Click, btnInsert.Click, btnDelete.Click
Select Case sender.name
Case "btnSelect" '查询
Show1()
Case "btnUpdate" '修改
SUpdate()
Case "btnInsert" '新增
SInsert()
Case "btnDelete" '删除
SDelete()
End Select
End Sub
'修改
Private Sub SUpdate()
txtNum.Text = Trim(txtNum.Text)
txtPrice.Text = Trim(txtPrice.Text)
If MsgBox("确定更新?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
SQLStr = "UPDATE t_icitem_new SET "
SQLStr = "fnumber='" cmbMonth.SelectedItem "', "
SQLStr = "fplanprice='" txtPrice.Text "' "
SQLStr = "WHERE fshortnumber='" txtNum.Text "'"
If Not SQLFill2(conn, SQLStr) Then
MsgBox("抱歉,更新失败:(")
End If
Show1()
End If
End Sub
'新增
Private Sub SInsert()
txtNum.Text = Trim(txtNum.Text)
txtPrice.Text = Trim(txtPrice.Text)
If MsgBox("确定新增?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
SQLStr = "SELECT fshortnumber "
SQLStr = "FROM t_icitem_new "
SQLStr = "WHERE fshortnumber = '" txtNum.Text "' "
SQLFill(conn, DA, DS, SQLStr, "Tmp")
If DS.Tables("Tmp").Rows.Count = 0 Then '如果没有资料则新增
SQLStr = "INSERT INTO t_icitem_new (fshortnumber,fnumber,fplanprice) VALUES("
SQLStr = "'" txtNum.Text "', "
SQLStr = "'" cmbMonth.SelectedItem "', "
SQLStr = "'" txtPrice.Text "')"
If Not SQLFill2(conn, SQLStr) Then
MsgBox("抱歉,新增失败:(")
End If
Show1()
Else
MsgBox("资料已存在")
End If
End If
End Sub
'删除
Private Sub SDelete()
txtNum.Text = Trim(txtNum.Text)
txtPrice.Text = Trim(txtPrice.Text)
If MsgBox("确定删除" txtNum.Text "?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2) = MsgBoxResult.Yes Then
SQLStr = "DELETE FROM t_icitem_new "
SQLStr = "WHERE fshortnumber='" txtNum.Text "'"
If Not SQLFill2(conn, SQLStr) Then
MsgBox("抱歉,删除失败:(")
End If
Show1()
End If
'键盘顺序
Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles txtFshop.KeyDown, txtItem.KeyDown
Select Case e.KeyCode
Case Keys.Return
SendKeys.Send("{TAB}")
Case Keys.Right
SendKeys.Send("{TAB}")
Case Keys.Left
SendKeys.Send("+{TAB}")
Case Keys.Down
SendKeys.Send("{TAB 10}")
Case Keys.Up
SendKeys.Send("+{TAB 10}")
End Select
End Sub
关于用户控件的使用,.net与v6环境下是有那么小小的区别。v6环境下的用户控件编写后可以直接使用,而在.net环境下,则需要先生成控件库(dll,应用程序扩展格式),然后再将这个控件库导入到.net编程环境的工具箱上方可使用。其具体方法如下(以VS 2010,Visual Basic.net 为例):1、新建项目-Visual Basic-Windows-Windows 窗体控件库-键入控件库名称(默认为Usercontrol1)确定;2、设计你的用户控件,完成后按“F7”进行编译;3、在该解决方案目录下找到控件库扩展(默认为UserControl1.dll),确认项目已经生成。在上三项为控件库DLL的生成方式,下面是调用方式:4、新建项目-Visual Basic-Windows-Windows 窗体应用程序-确定;5、打开工具箱,以任意一个控件上点击右键-选择项-.net framework组件-浏览-定位到生成的控件库扩展-确定;6、在工具箱中找到新添加的用户控件,将其拖至窗体中进行设计。经测,在.net环境下编译的用户控件是通用的。也就是说使用Visual Basic.net编写的用户控件,也可以在C#工程里使用,反之亦然。这充分体现了.net语言的交互性,在我看来,.net下各种语言能力相当,它们都是作为一种中间语言,编译出中间代码。所以,在.net环境下,采用混合编程也未尝不是一样提高效率的一种可选的方式。