如何使datagridviewcell的默认值为空 [英] How to make default value of datagridviewcell blank

查看:136
本文介绍了如何使datagridviewcell的默认值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个vb.net程序,它读入一个excel文件并将内容显示到datagridview中。我的目标是让代码告诉用户单元格中是否有除1或空白之外的任何值。然后我想为datagridviewcell提供一个默认值blank。如果默认值为1,我很容易就能完成所有这些操作,但当我将其更改为空(String.empty)时,程序将保持无效输入。这是我的代码如下。如果有人能弄清楚如何使程序提供一个空值作为默认值我会非常感激! :)



  Sub  validateDGV(rowindex,columnindex)

Dim value As String = DataGridView1.Rows(rowindex).Cells(columnindex).Value.ToString
If (columnindex = 1 然后
Dim cellData = DataGridView1.Rows(rowindex).Cells( columnindex)。值
如果 cellData 没有 OrElse IsDBNull(cellData) OrElse cellData.ToString = 字符串 .Empty 然后
' 不要因为这是允许的
ElseIf cellData<> 1 然后
MessageBox.Show( 值必须为1或空白
DataGridView1.Rows(rowindex).Cells(columnindex).Value = 字符串 .Empty ' 这应该提供空白的默认值回到我的datagridview但它的不是:(
退出 Sub

< span class =code-keyword>结束 如果
结束 如果
结束 Sub

解决方案

如果您的DGV与MS Excel Sheet数据绑定(使用OLEDB),则可能是,单元格的默认值应为 DBNull.Value

right now I have a vb.net program that reads in an excel file and displays the contents into a datagridview. My goal is to have code that tells the user if there is any value besides 1 or blank in the cell. Then I want to supply a default value of blank to the datagridviewcell. I was easily able to do all of this if the default value was 1 but when I change it to blank (String.empty) the program will keep the invalid input. Here is my code below. If anyone can figure out how to make the program supply a blank value as default value I would greatly appreciate it! :)

Sub validateDGV(rowindex, columnindex)

    Dim value As String = DataGridView1.Rows(rowindex).Cells(columnindex).Value.ToString
    If (columnindex = 1) Then
        Dim cellData = DataGridView1.Rows(rowindex).Cells(columnindex).Value
        If cellData Is Nothing OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
            'Do nothing because this is allowed
        ElseIf cellData <> 1 Then
            MessageBox.Show("Value must be 1 or Blank")
            DataGridView1.Rows(rowindex).Cells(columnindex).Value = String.Empty 'This should be supplying the default value of blank back to my datagridview but its not :(
            Exit Sub

        End If
    End If
End Sub

解决方案

If your DGV is bind with MS Excel Sheet data (using OLEDB), then probably, default value of cell should be DBNull.Value.


这篇关于如何使datagridviewcell的默认值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆