MSFlexGrid控件是用来显示和操作表格数据。在敲机房的时候,好几个窗体都用到了这个控件,它直观的向我们展示了符合条件的数据,我就小小的总结了一下它在机房中的应用。
Row | 当前单元格所在行 |
Col | 当前单元格所在列 |
Rows | 表格的行数 |
Cols | 表格的列数 |
Rowsel | 为一定范围的单元格返回或设置起始行、终止行 |
Colsel | 为一定范围的单元格返回或设置起始列、终止列 |
CellAignment | 设置单元格格式属性 |
TextMatrix | 设置单元格内容 |
例:在添加和删除用户的窗体中,根据用户级别这一条件,列出User_Info表中符合的数据。
Private Sub CmdUpdate_Click() '刷新数据 TxtSQL = "SELECT * From User_Info where Level='" & ComboLevel & "'" Set Mrc = ExecuteSQL(TxtSQL, Msgtext) '通过level选择,从表中调出信息 With FlexGrid1 '填写表头 .Rows = 1 .CellAlignment = 4 .TextMatrix(0, 0) = "用户名" .TextMatrix(0, 1) = "姓名" .TextMatrix(0, 2) = "开户人" Do While Not Mrc.EOF '遍历所有的记录,填入控件中 .Rows = .Rows + 1 .CellAlignment = 4 .TextMatrix(.Rows - 1, 0) = Trim(Mrc.Fields(0)) .TextMatrix(.Rows - 1, 1) = Trim(Mrc.Fields(3)) .TextMatrix(.Rows - 1, 2) = Trim(Mrc.Fields(4)) Mrc.MoveNext Loop End With Mrc.Close End Sub
例:在添加和删除用户窗体中,需要删除指定行的用户,
Private Sub CmdDelete_Click() '删除用户 Dim rstMrc As ADODB.Recordset If ComboLevel.Text = "" Then '判断表格中是否有内容 MsgBox "没有信息!", vbOKOnly + vbExclamation, "警告" Exit Sub End If If Not IsNumeric(Trim(FlexGrid1.TextMatrix(FlexGrid1.Row, 0))) Then '判断选中的用户名是否为数字,不是数字证明选中了表头 MsgBox "该行为表头,请重新进行选择!", vbOKOnly + vbExclamation, "警告" Exit Sub End If TxtSQL = "DELETE FROM User_Info where UserID='" & Trim(FlexGrid1.TextMatrix(FlexGrid1.Row, 0)) & "'" Set rstMrc = ExecuteSQL(TxtSQL, Msgtext) '将符合Userid条件的该项记录删除 FlexGrid1.RemoveItem FlexGrid1.Row '在表格控件中删除这一行 End Sub Private Sub FlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) With FlexGrid1 .Row = .MouseRow '把选中行的值给.row NowRow = .Row '把鼠标指定行给nowrow .Col = 0 .ColSel = .Cols - 1 End With End Sub Private Sub FlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) With FlexGrid1 .RowSel = NowRow '把行值给单元格内的起始终止行 .ColSel = .Cols - 1 End With End Sub
<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>Private Sub CmdModify_Click() '修改 If Not IsNumeric(Trim(FlexGrid.TextMatrix(FlexGrid.Row, 2))) Then '通过判断卡号这列是否为数字,来判断是否选中了记录 MsgBox "没有选定要修改的记录!", vbOKOnly + vbExclamation, "警告" Exit Sub End If SetParent frmModifys.hWnd, frmMain.hWnd '修改窗体弹出 Unload Me '卸载本窗体 End Sub </strong></span>
Private Sub Form_Load() '初始化窗体的大小 Me.Height = 8000 Me.Width = 10000 '根据学生信息维护窗体中FlexGrid中选择的卡号,获取学生信息 txtCardno.Text = Trim(frmMaintainStudent.FlexGrid.TextMatrix(frmMaintainStudent.FlexGrid.Row, 2)) TxtSQL = "SELECT * From Student_Info where cardno='" & Trim(txtCardno) & "'" Set Mrc = ExecuteSQL(TxtSQL, Msgtext) '将从Student表中获取的信息,填入文本框中 txtStudentno = Trim(Mrc.Fields(1)) txtStudentName = Trim(Mrc.Fields(2)) CboSex.Text = Trim(Mrc.Fields(3)) txtDepartment = Trim(Mrc.Fields(4)) txtGrade = Trim(Mrc.Fields(5)) txtClass = Trim(Mrc.Fields(6)) txtCash = Trim(Mrc.Fields(7)) txtStatus = Trim(Mrc.Fields(10)) txtMsg = Trim(Mrc.Fields(8)) CboType.Text = Trim(Mrc.Fields(14)) End Sub
Private Sub CmdExcel_Click() '导入Excel表格 Dim xlApp As Excel.Application '定义一个Excel应用程序 Dim xlBook As Excel.Workbook '定义一个工作簿 Dim xlSheet As Excel.Worksheet '定义一个工作表 Dim xlRange As Excel.Range '定义一个单元格范围 Dim i As Integer '定义横坐标 Dim j As Integer '定义纵坐标 Set xlApp = CreateObject("Excel.application") '创建Excel应用程序对象 Set xlBook = xlApp.Workbooks.Add '创建一个工作簿 Set xlSheet = xlBook.Worksheets(1) '创建一个工作表 DoEvents '转让控制权,以便让操作系统处理其它的事件 With FlexGrid '将FlexGrid控件中内容导入Excel表格中 For i = 0 To .Rows - 1 '遍历所有的行 For j = 0 To .Cols - 1 '遍历所有的列 DoEvents '转让控制权,以便让操作系统处理其它的事件 xlApp.ActiveSheet.Cells(i + 1, j + 1) = .TextMatrix(i, j) '创建新的单元格,并添加FlexGrid中的内容 Next j Next i End With xlApp.ActiveWorkbook.SaveAs App.Path & "\学生上机信息查询.xls" '表格保存路径 xlApp.ActiveWorkbook.Saved = True '保存表格 MsgBox "导出完成!", vbOKOnly + vbExclamation, "提示" '保存成功提示信息 xlApp.Visible = True '显示表格 End Sub