我希望像文本框一样清除datagridview单元格并保持焦点 [英] I want datagridview cell to be cleared like text box and remain focused

查看:125
本文介绍了我希望像文本框一样清除datagridview单元格并保持焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview,其中包含4列Item,Qty,Price,Amount.用户应在第一栏中输入商品名称.我在数据库中有一个表,其中存储了所有项目名称.因此,应检查插入的名称是否在数据库中.如果它不在数据库中,我希望像文本框一样清除该单元格,并将焦点保留在该单元格中.应该允许用户继续Continuo,直到他输入正确的商品名称为止.我最大的问题是使该单元格保持聚焦状态
到目前为止,我已经尝试过了

我尝试过的事情:

私有子DataGridView1_CellEndEdit(ByVal发送者作为对象,ByVal e作为System.Windows.Forms.DataGridViewCellEventArgs)处理DataGridView1.CellEndEdit

connect()
sql =从其中ProductName ="的产品中选择ProductName"& Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString& ""
objcmd =新的SqlCommand(sql,objcon)
OBJDR = objcmd.ExecuteReader
如果OBJDR.Read = False,则
''''显示此消息
MessageBox.Show(项目名称不在项目列表中",项目名称错误",MessageBoxButtons.OK,MessageBoxIcon.Error)
清除单元格"
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value ="
DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0)
''DataGridView1.BeginEdit(True)
退出子
如果结束
end sub

I have a datagridview that contains 4 columns Item,Qty,Price,Amount. the user should enter the item name in the first column. i have a table in the database that all the item names are stored. so the inserted name should be checked if its in database or not. if its not in the databse i want the cell to be cleared like a textbox and the focus remain in that cell. the user should be allowed to Continuo until he enters the correct item name... my biggest problem is making that cell to remain focused
so far i have tried this

What I have tried:

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

connect()
sql = "Select ProductName from Products where ProductName = ''" & Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString & "'' "
objcmd = New SqlCommand(sql, objcon)
OBJDR = objcmd.ExecuteReader
If OBJDR.Read = False Then
'''' display this message
MessageBox.Show("Item Name Is not in the Items list", "Item Name Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
"clear the cell ''
Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value = ""
DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(0)
'' DataGridView1.BeginEdit(True)
Exit Sub
End If
end sub

推荐答案

尝试将您的代码放入单元格验证事件中

try to put your code in cell validating event

Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
       With DataGridView1.Rows(e.RowIndex)
           If e.ColumnIndex = 0 Then
               If Not ItemExists(.Cells(e.ColumnIndex).Value) Then
                   MessageBox.Show("Item Name Is not in the Items list", "Item Name Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                   e.Cancel = True
               End If
           End If
       End With
   End Sub


Function ItemExists(ByVal itemname As String) As Boolean
      Dim bResult As Boolean = True

      connect()
      Sql = "Select ProductName from Products where ProductName = '" & itemname & "' "
      objcmd = New SqlCommand(Sql, objcon)
      OBJDR = objcmd.ExecuteReader
      If OBJDR.Read = False Then
          bResult = False
      End If

      Return bResult
  End Function



这篇关于我希望像文本框一样清除datagridview单元格并保持焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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