Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I'm trying to have my program give the user a message that tells them if they enter the number zero or leave the datagridviewcell blank. Right now the columns in my datagridview are populated by numbers that come from an excel spreadsheet. I not only want to display a message, but after the message pops up I want to supply a default value for the cell like "NA" that will fill the datagridviewcell. Here is what I have so far.

VB
If (e.ColumnIndex = 2) Then 'checking value for column 2 only
            Dim cellData = DataGridViewC1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
            If cellData <= 0 OrElse IsDBNull(cellData) OrElse cellData.ToString = String.Empty Then
                MessageBox.Show("Number Must Be Greater Than Zero")
                DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = "NA"
            End If
        End If
Posted
Comments
CHill60 20-Jun-13 20:56pm    
So if this doesn't work what is the error message or problem?

1 solution

If you are using Visual Studio, you can use one of the Validation controls for your DataGridView column. And it would be Range Validator control. You can refer to http://msdn.microsoft.com/en-us/library/f70d09xt(v=vs.71).aspx[^]
 
Share this answer
 
v2
Comments
CAS1224 21-Jun-13 10:30am    
I'm using VB.net 4.5, this is ASP.net. Is there a VB version of this?
Sruthi Jain 24-Jun-13 10:31am    
Yes, it is all there in Google. Refer to http://www.exforsys.com/tutorials/vb.net-2005/vb.net-validation.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900