Hi kana250688,
Refer below example created using Mudassar sir's shared code to verify whether the row have empty or zero value.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Age",typeof(int))
});
dt.Rows.Add(1, "John Hammond", 57);
dt.Rows.Add(2, "Mudassar Khan", 39);
dt.Rows.Add(3, "Suzanne Mathews", 0);
dt.Rows.Add(4, "", 25);
this.dataGridView1.DataSource = dt;
this.dataGridView1.AllowUserToAddRows = false;
}
private bool CheckIfEmpty(DataGridViewRow rw)
{
for (int i = 0; i <= rw.Cells.Count - 1; i++)
{
if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value
|| string.IsNullOrEmpty(rw.Cells[i].Value.ToString()) || rw.Cells[i].Value.ToString() == "0")
{
return true;
}
}
return false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab && Convert.ToBoolean(Keys.Enter))
{
if (this.dataGridView1.Rows.Count > 0)
{
if (this.CheckIfEmpty(dataGridView1.CurrentRow))
{
dataGridView1.CurrentRow.Selected = true;
}
else
{
dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells["Id"];
dataGridView1.BeginEdit(false);
}
}
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Age", GetType(Integer))})
dt.Rows.Add(1, "John Hammond", 57)
dt.Rows.Add(2, "Mudassar Khan", 39)
dt.Rows.Add(3, "Suzanne Mathews", 0)
dt.Rows.Add(4, "", 25)
Me.dataGridView1.DataSource = dt
Me.dataGridView1.AllowUserToAddRows = False
End Sub
Private Function CheckIfEmpty(ByVal rw As DataGridViewRow) As Boolean
For i As Integer = 0 To rw.Cells.Count - 1
If rw.Cells(i).Value Is Nothing OrElse IsDBNull(rw.Cells(i).Value) _
OrElse String.IsNullOrEmpty(rw.Cells(i).Value.ToString()) OrElse rw.Cells(i).Value.ToString() = "0" Then
Return True
End If
Next
Return False
End Function
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Tab AndAlso Convert.ToBoolean(Keys.Enter) Then
If Me.dataGridView1.Rows.Count > 0 Then
If Me.CheckIfEmpty(dataGridView1.CurrentRow) Then
dataGridView1.CurrentRow.Selected = True
Else
dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells("Id")
dataGridView1.BeginEdit(False)
End If
End If
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Screenshot