| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3199 人关注过本帖
标题:用Vb.net 2005郁闷死了!
只看楼主 加入收藏
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
 问题点数:0 回复次数:10 
用Vb.net 2005郁闷死了!

问题1: DataGridView的AllowUserToAddRows=True时,如果把光标移入其最后的新行(空白行)后(没有输入任何内容,此时也DataGridView也没有自动增加一新行),如果再执行Me.sqlAdapter.Update(sqlDataSource.DataSource)则出错,说是无法将NUll值插入某某列(该列确实不能为空),问题是前段时间我都是用同样的方法做了十几个这功能的窗口调试时都没有没错,今天一运行就出现这样的错误,问题何在?

问题2:Try
Me.sqlAdapter.Update(sqlDataSource.DataSource)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")
End Try
当有错时 MsgBox(ex.Message, MsgBoxStyle.Exclamation, "错误")的提示信息一闪而过(前段时间都不会),我原来用Vb.net 2003设计ASP.net程序时开始设计阶段不会,后来也有这样的问题,问题何在?

问题1和问题2都是今天才发生的!

搜索更多相关主题的帖子: Update DataGridView DataSource sqlAdapter Exception 
2007-11-01 13:27
qlong0728
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:272
专家分:0
注 册:2007-6-15
收藏
得分:0 
编程思路不够清晰,代码不够精密,构思不够成熟

程序人员写程序,又拿程序换酒钱。 奔驰宝马贵者趣,公交自行程序员。 不见满街漂亮妹,哪个归得程序员。
2007-11-01 15:02
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 
?
2007-11-01 15:46
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 


以下是代码,帮我看看,谢谢!

另,该窗口如果做为子窗口在父窗口里运行,还有个问题,请参见我之前提的“程序为什么会没有响应?”

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编辑过]

2007-11-01 16:28
qlong0728
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:272
专家分:0
注 册:2007-6-15
收藏
得分:0 
Dim sqlConn As SqlConnection
应改为
Dim sqlConn As New SqlClient.SqlConnection


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")
这段如是换成我写,我会这样写
Dim daaaa As New SqlClient.SqlDataAdapter
SQLtile = "Select bankNumber,bankName,bankAccount,bankMaster From DEBK"
daaaa.SelectCommand = New SqlClient.SqlCommand(SQLtile, sqlConn)
daaaa.Fill(sqlDataSet)
DataGridView1.DataSource = sqlDataSet.Tables(0)
其它的自己慢慢检查一下,在定义时,有些是需要加AS NEW的或加上AS NEW SqlClient的

只供参考....

程序人员写程序,又拿程序换酒钱。 奔驰宝马贵者趣,公交自行程序员。 不见满街漂亮妹,哪个归得程序员。
2007-11-02 08:28
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 

谢谢您的建议!

之所以没这样写:
SQLtile = "Select bankNumber,bankName,bankAccount,bankMaster From DEBK"
daaaa.SelectCommand = New SqlClient.SqlCommand(SQLtile, sqlConn)
因为觉得命令字符串不是太长并不影响编写和查看的话没必要定义一个变量来存储(命令比较长的话我也是采用这样的方法的),定义一个变量来存储不是要消耗内存资源的吗?(虽然消耗得不多,但是能节约就节约)

另外,有的申明要用NEW关键字这是肯定的了,没用的话程序就不会执行了,之所以省略了sqlClient那是因为我在项目里已经导入了System.Data.SqlClient命名空间了.

这些暂且不说,我只想知道我提的问题怎么解决,不过问题2在我重装了操作系统和软件后没有出现了,但问题1我觉得不是代码的问题,应该是我对DataGridView控件还不太熟悉,不了解它的一些执行机制,比方说:

1、DataGridView在允许用户添加行时,在什么情况下添加行有效(这是问题1的关键)

2、DataGridView在邦定数据源后是不能通过DataGridView.Rows.Add还添加行的,在AllowUserToAddRow=False的情况下,要如何添加新行呢?(虽然可以用BindingNavigator来添加,但在不用BindingNavigator时又该如何添加呢?)

[此贴子已经被作者于2007-11-2 9:29:36编辑过]

2007-11-02 09:26
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 

图片附件: 游客没有浏览图片的权限,请 登录注册

上面是该窗口的截图。

发现了问题所在:

当DataGridView的EditMode=EditOnEnter时,只要将光标移到空白行的任何单元格内(没有输入内容),DataGridView并不会再新增加一新行,但DataSet还是会自动新增加一行,如果没有在单元格内输入任何内容,那么该行就是一空行,最后导致再更新时出现了问题,而且是每一次这样的操作就新增加一行。(现在也就知道为什么这样操作一次,再进行排序时会多出一行空白行来了)

当DataGridView的EditMode=EditOnKeystrokeOrF2 或 EditOnKeystroke时,以上现象不会发生。

但问题是我记得之前就是在EditMode=EditOnEnter时都没会有这种情况,会不会还有其它设置会影响到?

谢谢!

[此贴子已经被作者于2007-11-2 10:36:38编辑过]

2007-11-02 10:21
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 

问题找到了:

'单元格退出编辑模式时
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

关键就是这句: Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString.Trim

要是没有这句就不会出问题了,但If e.RowIndex = Me.DataGridView1.Rows.Count - 1 Then Exit Sub 已经有个开关,如果是最后一行(即空白行)就不会执行后面的语句了,怎么还会影响到呢?

2007-11-02 11:15
qlong0728
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:272
专家分:0
注 册:2007-6-15
收藏
得分:0 
If e.RowIndex = Me.DataGridView1.Rows.Count - 2 Then Exit Sub

程序人员写程序,又拿程序换酒钱。 奔驰宝马贵者趣,公交自行程序员。 不见满街漂亮妹,哪个归得程序员。
2007-11-02 11:43
homesite
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2007-9-27
收藏
得分:0 
回复:(qlong0728)If e.RowIndex = Me.DataGridView...

这样不对啦,那样的话非空最后一行就不能被操作了(要的只是忽略最后那个空白行!)。
我的做法把出问题的那行代码放到了DataGridView1.CellValueChanged事件中就行了。

当然问题还是很奇怪的,有点奇怪,不知是不是Bug?

另外该窗口为什么在做为子窗口运行时,当行验证信息不全时会发生程序没有响应,CPU占用效达到95%以上的问题也找到了,看下面代码:

'验证一行的“银行编号”、“开户行”、“帐号”是否为空
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


从表面上看没点问题,事实在如果该窗口做为独立窗口(即非MDI的子窗口)来运行也是不存在问题的,但现在的问题却是因为e.Cancel=True引起的,e.Cancel=True应该是用来表示行验证没有通过的,现在去掉e.Cancel=True语句就一切正常了,不知何故?会不会是Bug问题?

2007-11-02 13:12
快速回复:用Vb.net 2005郁闷死了!
数据加载中...
 
   



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

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