DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确...

其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊。报错如下:

 

 

 

---------------------------
“DataGridView 默认错误”对话框
---------------------------
DataGridView 中发生以下异常:

System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。
   在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   在 System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
   在 System.ComponentModel.DecimalConverter.FromString(String value, NumberFormatInfo formatInfo)
   在 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   在 System.ComponentModel.TypeConverter.ConvertFrom(Object value)
   在 System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)

要替换此默认对话框,请处理 DataError 事件。
---------------------------
确定   
---------------------------

解决方法,注册一个DGV_DataError事件就可以。

private void DGV_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

  //可以不执行代码哦

}

 

转载于:https://www.cnblogs.com/haibing0107/p/7614644.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于SQL语句的参数没有被正确指定而引起的。在这个例子,问题可能出在SQL语句的`Label3.Text`参数上。为了解决这个问题,您可以尝试使用`OleDbCommand`对象和参数化查询来执行查询。下面是一个修改后的代码示例: ``` Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Andrew\OneDrive\文件\研究生\油水\实验数据处理\油水分离实验数据数据库.mdb;") Dim query As String = "SELECT * FROM 含乳化剂 WHERE 分离效率 > ?" Dim adapter As New OleDbDataAdapter(query, connection) adapter.SelectCommand.Parameters.AddWithValue("@分离效率", Label3.Text) Dim dataset As New DataSet() adapter.Fill(dataset, "含乳化剂") DataGridView1.DataSource = dataset.Tables("含乳化剂") If DataGridView1.Rows.Count > 0 Then For Each row As DataGridViewRow In DataGridView1.Rows If row.Cells("分离效率").Value.ToString().Equals(Label3.Text) Then row.Selected = True Exit For End If Next Else MessageBox.Show("数据库未查询到") Me.Close() Label3.Text = "" End If ``` 在这个例子,我们使用`OleDbCommand`对象和参数化查询来执行查询。在SQL语句,我们使用`?`来表示参数,然后在`adapter.SelectCommand.Parameters`集合添加相应的参数。在这种情况下,我们使用`AddWithValue`方法来添加一个参数,该参数的名称为`@分离效率`,值为`Label3.Text`。这样就可以将参数正确地指定到SQL语句,避免了“至少一个参数没有被指定值”的错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值