以下是代码,帮我看看,谢谢!
另,该窗口如果做为子窗口在父窗口里运行,还有个问题,请参见我之前提的“程序为什么会没有响应?”
Public Class frmDefineBank
Dim blnLoadForm As Boolean
Dim sqlConn As SqlConnection
Dim sqlCommand As SqlCommand
Dim sqlAdapter As SqlDataAdapter
Dim sqlDataSet As New DataSet
Dim sqlDataSource As New BindingSource
Dim blnContextChanged, blnRowValidated As Boolean
Dim commandBuilder As SqlCommandBuilder
'退出窗体时提示是否保存
Private Sub frmDefineBank_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If blnContextChanged Then
If Not blnRowValidated Then
e.Cancel = True
Else
Select Case publicfunIsSaveOrCancel()
Case MsgBoxResult.Yes
Try
Me.sqlAdapter.Update(sqlDataSource.DataSource)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")
End Try
Case MsgBoxResult.Cancel
e.Cancel = True
Exit Sub
End Select
End If
End If
End Sub
'按ESC键关闭窗口
Private Sub frmDefineBank_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = 27 Then
If blnContextChanged Then
If Not blnRowValidated Then
Exit Sub
Else
Select Case publicfunIsSaveOrCancel()
Case MsgBoxResult.Yes
Try
Me.sqlAdapter.Update(sqlDataSource.DataSource)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")
End Try
Case MsgBoxResult.Cancel
Exit Sub
End Select
End If
End If
blnContextChanged = False
Me.Close()
End If
End Sub
Private Sub frmDefineBank_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
blnLoadForm = True
blnRowValidated = True
Try
sqlConn = New SqlConnection(sqlConString)
sqlCommand = New SqlCommand("Select bankNumber,bankName,bankAccount,bankMaster From DEBK", sqlConn)
sqlAdapter = New SqlDataAdapter(sqlCommand)
commandBuilder = New SqlCommandBuilder(sqlAdapter)
sqlAdapter.Fill(sqlDataSet, "DEBK")
'将DataGridView控件邦定到指定的数据表中
sqlDataSource.DataSource = sqlDataSet.Tables("DEBK")
Me.DataGridView1.DataSource = sqlDataSource
Dim columnBankNumber As New DataGridViewTextBoxColumn
Dim columnBankName As New DataGridViewTextBoxColumn
Dim columnBankAccount As New DataGridViewTextBoxColumn
Dim columnBankMaster As New DataGridViewTextBoxColumn
With columnBankNumber
.Name = "银行编号"
.MaxInputLength = 8
.DataPropertyName = "bankNumber"
.SortMode = DataGridViewColumnSortMode.NotSortable
End With
With columnBankName
.Name = "开户行"
.MaxInputLength = 50
.DataPropertyName = "bankName"
.SortMode = DataGridViewColumnSortMode.NotSortable
End With
With columnBankAccount
.Name = "帐号"
.MaxInputLength = 50
.DataPropertyName = "bankAccount"
.SortMode = DataGridViewColumnSortMode.NotSortable
End With
With columnBankMaster
.Name = "开户方"
.MaxInputLength = 50
.DataPropertyName = "bankMaster"
.SortMode = DataGridViewColumnSortMode.NotSortable
End With
Me.DataGridView1.Columns("bankNumber").Visible = False
Me.DataGridView1.Columns("bankName").Visible = False
Me.DataGridView1.Columns("bankAccount").Visible = False
Me.DataGridView1.Columns("bankMaster").Visible = False
Me.DataGridView1.Columns.Add(columnBankNumber)
Me.DataGridView1.Columns.Add(columnBankName)
Me.DataGridView1.Columns.Add(columnBankAccount)
Me.DataGridView1.Columns.Add(columnBankMaster)
Me.DataGridView1.Columns("银行编号").MinimumWidth = 80
Me.DataGridView1.Columns("开户行").MinimumWidth = 130
Me.DataGridView1.Columns("帐号").MinimumWidth = 130
Me.DataGridView1.Columns("开户方").MinimumWidth = 130
Me.DataGridView1.Columns("银行编号").Width = 80
Me.DataGridView1.Columns("开户行").Width = 130
Me.DataGridView1.Columns("帐号").Width = 130
Me.DataGridView1.Columns("开户方").Width = 130
Me.DataGridView1.Columns("开户方").AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")
End Try
blnLoadForm = False
End Sub
'验证一行的“银行编号”、“开户行”、“帐号”是否为空
Private Sub DataGridView1_RowValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
If blnLoadForm Then Exit Sub '加载窗体中跳过行验证
If e.RowIndex = Me.DataGridView1.Rows.Count - 1 Then Exit Sub
If String.IsNullOrEmpty(Me.DataGridView1.Rows(e.RowIndex).Cells("银行编号").Value.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).Cells("银行编号").Selected = True
Me.DataGridView1.BeginEdit(True)
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "银行编号不能为空"
blnRowValidated = False
e.Cancel = True
Exit Sub
End If
If String.IsNullOrEmpty(Me.DataGridView1.Rows(e.RowIndex).Cells("开户行").Value.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).Cells("开户行").Selected = True
Me.DataGridView1.BeginEdit(True)
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "开户行不能为空"
blnRowValidated = False
e.Cancel = True
Exit Sub
End If
If String.IsNullOrEmpty(Me.DataGridView1.Rows(e.RowIndex).Cells("帐号").Value.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).Cells("帐号").Selected = True
Me.DataGridView1.BeginEdit(True)
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "帐号不能为空"
blnRowValidated = False
e.Cancel = True
Exit Sub
End If
blnRowValidated = True
End Sub
'验证单元格是否为空,并验证“银行编号”是否唯一
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.RowIndex = Me.DataGridView1.Rows.Count - 1 Then Exit Sub
Select Case e.ColumnIndex
Case 4
If String.IsNullOrEmpty(e.FormattedValue.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "银行编号不能为空"
e.Cancel = True
Else
Dim i As Integer
For i = 0 To Me.DataGridView1.Rows.Count - 2
If e.RowIndex = i Then Continue For
If Me.DataGridView1.Rows(i).Cells("银行编号").Value.ToString.ToUpper.Trim = e.FormattedValue.ToString.ToUpper.Trim Then
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "银行编号已定义"
e.Cancel = True
Exit Sub
End If
Next
End If
Case 5
If String.IsNullOrEmpty(e.FormattedValue.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "开户行不能为空"
e.Cancel = True
End If
Case 6
If String.IsNullOrEmpty(e.FormattedValue.ToString.Trim) Then
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "帐号不能为空"
e.Cancel = True
End If
End Select
End Sub
'单元格内容更改时标记“已更改“
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
' blnContextChanged是用来记录内容的更改;
'加载窗体中跳过
If blnLoadForm Then Exit Sub
blnContextChanged = True
End Sub
'单元格退出编辑模式时
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
'截掉单元格内容的左右空格字符
If e.RowIndex = Me.DataGridView1.Rows.Count - 1 Then Exit Sub
Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString.Trim
Me.DataGridView1.Rows(e.RowIndex).ErrorText = Nothing
End Sub
'确定按钮事件
Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
If Not blnRowValidated Then Exit Sub
If blnContextChanged Then
Try
Me.sqlAdapter.Update(sqlDataSource.DataSource)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")
End Try
End If
blnContextChanged = False
Me.Close()
End Sub
'关闭按钮事件
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
If Not blnRowValidated Then Exit Sub
If blnContextChanged Then
If publicFunIsSave() Then
Try
Me.sqlAdapter.Update(sqlDataSource.DataSource)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")
End Try
End If
End If
blnContextChanged = False
Me.Close()
End Sub
End Class
[此贴子已经被作者于2007-11-1 16:32:38编辑过]