//给窗体加点击按钮列
Public btn As DataGridViewButtonColumn
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Sy.btn.Name = "操作"
Sy.btn.HeaderText = "操作"
Sy.btn.DefaultCellStyle.NullValue = "详情"
DataGridView1.Columns.Add(Sy.btn)
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim Gindex As Integer = DataGridView1.CurrentRow.Index
Sytem.Rn = DataGridView1.Item(2, Gindex).Value.ToString
End Sub
'DataGridView导出到Excel(添加引用Net选项卡勾选Microsoft.Excel 14.0 Object Library[Microsoft.Office.Interop.Excel](通用Vs05-Vs10))
Private Sub ExportToExcel(ByVal MyGridview As DataGridView, ByVal Excel_name As String)
' Creating a Excel object.
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
Try
worksheet = workbook.ActiveSheet
worksheet.Name = Excel_name
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'Loop through each row and read value from each column.
For i As Integer = 0 To MyGridview.Rows.Count - 2
For j As Integer = 0 To MyGridview.Columns.Count - 1
' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
If cellRowIndex = 1 Then
worksheet.Cells(cellRowIndex, cellColumnIndex) = MyGridview.Columns(j).HeaderText
Else
worksheet.Cells(cellRowIndex, cellColumnIndex) = MyGridview.Rows(i).Cells(j).Value.ToString()
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next
'Getting the location and file name of the excel to save from user.
Dim saveDialog As New SaveFileDialog()
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
saveDialog.FilterIndex = 2
If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
workbook.SaveAs(saveDialog.FileName)
MessageBox.Show("Export Successful")
End If
Catch ex As System.Exception
MessageBox.Show(ex.Message)
Finally
excel.Quit()
workbook = Nothing
excel = Nothing
End Try
End Sub
'限制第2列只能输入数字问题
Public EditCell As DataGridViewTextBoxEditingControl
Private Flag As Boolean '焦点是否在第二列的判断标志
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If Me.DataGridView1.CurrentCellAddress.X = 1 Then
EditCell = CType(e.Control, DataGridViewTextBoxEditingControl)
EditCell.SelectAll()
AddHandler EditCell.KeyPress, AddressOf Cells_KeyPress
Flag = True
Else
Flag = False
End If
End Sub
Private Sub Cells_KeyPress(sender As System.Object, e As KeyPressEventArgs)
If Flag = True Then
If e.KeyChar <> Chr(8) And e.KeyChar <> Chr(13) And (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) Then
Beep()
Beep()
e.KeyChar = Chr(0)
End If
End If
End Sub
'方法Mouseup
'Private Sub DataGridView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp
'方法一:最优方式 捕捉空值
Dim Gindex As Integer = DataGridView1.CurrentRow.Index '将选择行的索引赋给变量
For i As Integer = 1 To 2 '遍历2行数据
If Convert.IsDBNull(DataGridView1.Item(i, Gid).Value) = False Then '点到空行的DBNull判断
TextBox1.Text = DataGridView1.Item(i, Gindex).Value
'TextBox2.Text = DataGridView1.Item("用户名", Gindex).Value
Else
TextBox1.Text = Nothing
TextBox2.Text = Nothing
End If
Next
End Sub
'方法二:捕捉错误的方式效率低下
Try
Dim Gindex As Integer = DataGridView1.CurrentRow.Index '将选择行的索引赋给变量
TextBox1.Text = DataGridView1.Item(0, Gindex).Value
TextBox2.Text = DataGridView1.Item("用户名", Gindex).Value
Catch ex As Exception
TextBox1.Text = Nothing
TextBox2.Text = Nothing
Exit Sub
End Try
//鼠标移动到网格控件弹出提示当前单元格值
Private Sub DataGridView1_CellToolTipTextNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellToolTipTextNeededEventArgs) Handles DataGridView1.CellToolTipTextNeeded
'方法一
If e.RowIndex > -1 And e.RowIndex < DataGridView1.RowCount - 1 Then '如果行值不在-1和最大行才执行
'e.ToolTipText = e.ColumnIndex.ToString() + ", " + e.RowIndex.ToString()
e.ToolTipText = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
End If
End Sub
'注意false不一定是空值,所以必须是双层是非判断(DataGrid空值的循环判断)
If Not Convert.IsDBNull(订单DataGridView.Rows(i).Cells(6).Value) AndAlso Convert.ToBoolean(订单DataGridView.Rows(i).Cells(6).Value) = True Then
Debug.Print(Convert.ToBoolean(订单DataGridView.Rows(i).Cells(6).Value).ToString())
End If
'不同控件共用过程(控件类型转换后添充数据)
Public Shared Sub GetList(ByVal LV As Control)
For i As Integer = 0 To SqlHelper.DB.DT.Rows.Count - 1
If LV.GetType.Name.ToString() = "ListView" Then
CType(LV, ListView).Items.Add(SqlHelper.DB.DT(i)(1).ToString())
Else
LV = CType(LV, ComboBox)
CType(LV, ComboBox).Items.Add(SqlHelper.DB.DT(i)(1).ToString())
End If
Next
End Sub
'用重载忽略控件类型
Public Overloads Shared Sub GetList(ByVal LV As ListView)
For i As Integer = 0 To SqlHelper.DB.DT.Rows.Count - 1
LV.Items.Add(SqlHelper.DB.DT(i)(1).ToString())
Next
End Sub
Public Overloads Shared Sub GetList(ByVal LV As ComboBox)
For i As Integer = 0 To SqlHelper.DB.DT.Rows.Count - 1
LV.Items.Add(SqlHelper.DB.DT(i)(1).ToString())
Next
End Sub
使用控件面板中滚动控件ScrollBar
Private Sub $hscroll$_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles $hscroll$.ValueChanged
Me.$panel$.Left = -Me.$hscroll$.Value
End Sub
Private Sub $vscroll$_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles $vscroll$.ValueChanged
Me.$panel$.Top = -Me.$vscroll$.Value
End Sub
'一次性清空Text Panel2.Controls
Public Shared Sub Clear(ByVal Ctl As Control)
For Each ctrl In Ctl.Controls
If ctrl.GetType.Name = "TextBox" Then
ctrl.Text = ""
End If
Next
End Sub
comboBox下拉快捷键
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
'SendKeys.Send("{F4}")'方法一
e.KeyChar = "{F4}" '方法二
End Sub