【问题标题】:Unable to create blank cell in a datagrid view无法在 datagridview 中创建空白单元格
【发布时间】:2014-03-08 11:25:52
【问题描述】:

我正在使用 4 列未绑定的数据网格视图。网格中填充了 Excel 工作表中的十进制数字。有些单元格有数字,有些则留空。

网格设置为编辑模式使用:

dgv.EditMode = DataGridViewEditMode.EditOnEnter

  • 如果我选择一个包含数字的单元格并将其替换为另一个 号码,一切都很好。
  • 如果我选择一个空白单元格并将其替换为一个数字,一切都很好。
  • 如果我选择一个包含数字的单元格并删除该数字,我会得到一个 错误消息,在输入数字之前我无法离开单元格。
  • 如果我选择一个空白单元格,然后尝试选择另一个单元格,我会得到一个 错误消息,在输入数字之前我无法离开单元格。

我需要能够让单元格带有数字并且单元格留空。

这是当我尝试将单元格保留为空白值时出现的错误消息。

DataGridView 出现以下异常:

system.FormatException:输入字符串的格式不正确。
---> system.FormatException:输入字符串不正确 格式。

在 System.Number.StringToNumber{string str, NumberStyles 选项,NumberBuffer& 数字,NumberFormatInfo 信息, Boolean parseDecimal)
在 System.Number.ParseDecimal {字符串值,Numberstyles 选项,NumberFormatInfo numfmt)
--- 内部异常堆栈跟踪结束 ---

在 system.windows.Forms.Formatter.lnvokeString 解析方法 {obje ct 值,Type targetType,IFormatProvider formatInfo)

在 system.windows.Forms.Formatter.ParseObjectInternal(对象 值,类型目标类型,类型源类型,类型转换器 targetConverter, Typeconverter sourceconverter, IForm atprovider formatInfo, 对象格式化 NullValue)

在 system.windovs.Forms.Formatter.parseobject(对象值, 输入 targetType、输入 sourceType、TypeConverter targetConverter, TypeConverter sourceConverter, iForm atprovider 格式信息,对象格式化空值,对象 dataSourceNullValue)

在 System.Windows.Forms.DataGridViewCell.ParseFormattedValueln terna(TypevalueType, 对象格式化值, DataGridViewCell.Style cellStyle, TypeConverter 格式化的ValueTypeConverter,类型转换器 valueTypeConverter)

在 system.windows.Forms.DataGridvieCell.ParseFormattedvalue( 对象 formattedValue, DataGridViewCellStyle cellStyle, Typeconverter formattedvalueTypeconverter, Typeconverter valueTypeConverter)

在 system.windows.Forms.DataGridview.pushFormattedvalue(数据 Gridviewcell&dataGridViewCurrentCell,对象格式化值, 异常&异常)

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

我相信此消息与无法将空白值“提交”到数据表有关。

这是用于填充数据网格视图的代码:

Dim cb As New OleDbConnectionStringBuilder With {.DataSource = XLFILENAMEANDPATH, .Provider = "Microsoft.ACE.OLEDB.12.0"}
cb.Add("Extended Properties", "Excel 12.0; IMEX=1; HDR=Yes;")

Dim cn As New System.Data.OleDb.OleDbConnection With {.ConnectionString = cb.ConnectionString}

cn.Open()

dta = New OleDbDataAdapter("Select * From [" & ActName & "$B6:E" & LastEntryRow & "]", cn)

dts = New DataSet

dta.Fill(dts, "Detailtable")

DataGridView1.DataSource = dts

DataGridView1.DataMember = "Detailtable"

cn.Close()

我无法解决问题,希望得到一些指导。

【问题讨论】:

  • 您是在创建列还是自动生成列?
  • 感谢您重新格式化。我是该网站的新手。
  • 这些列是从连接中自动生成的。
  • 您是否使用 Parse.Decimal 解析代码中任何位置的单元格值?同时提供错误堆栈跟踪。
  • 没有。我已将代码完全剥离回上面填充 datagridview 的代码。

标签: vb.net datagridview


【解决方案1】:

使用小数时,这些错误经常与文化特定问题有关。大多数.net 函数的构建,其中返回值是“文化相关的”,使用CurrentThread.CurrentCulture 来格式化它的返回值(如果没有其他指定)。以下是来自Decimal.Parse 的示例:

Public Structure Decimal
    '...
    Public Shared Function Parse(ByVal s As String, ByVal style As NumberStyles) As Decimal
        NumberFormatInfo.ValidateParseStyleFloatingPoint(style)
        Return Number.ParseDecimal(s, style, NumberFormatInfo.CurrentInfo)
    End Function
    '...
End Structure

NumberFormatInfo.CurrentInfo 在哪里:

Public Shared ReadOnly Property CurrentInfo As NumberFormatInfo
    Get
        Dim currentCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
        If Not currentCulture.m_isInherited Then
            Dim numInfo As NumberFormatInfo = currentCulture.numInfo
            If (Not numInfo Is Nothing) Then
                Return numInfo
            End If
        End If
        Return DirectCast(currentCulture.GetFormat(GetType(NumberFormatInfo)), NumberFormatInfo)
    End Get
End Property

您需要指定当前线程的正确CultureInfo。一种方法是在主窗体的构造函数中设置它。在挪威(我来自哪里),我们使用, 作为小数分隔符,而不是.。如果我不改变文化,这将导致很多问题。因此,在以下示例中,文化设置为挪威语:

Public Class Program

    Public Sub New()
         Me.InitializeComponent()
        Thread.CurrentThread.CurrentCulture = New CultureInfo("nb-NO")
        Thread.CurrentThread.CurrentUICulture = New CultureInfo("nb-NO")
     End Sub

End Class

以下是文化代码列表,您可以在其中找到自己的代码:

http://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx

【讨论】:

  • 感谢大家的帮助。我最终通过设置 DataGridView1.DefaultCellStyle.NullValue = "" 解决了这个问题
猜你喜欢
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2021-04-20
  • 2011-10-25
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2013-06-12
相关资源
最近更新 更多