Link to home
Start Free TrialLog in
Avatar of SmashAndGrab
SmashAndGrab

asked on

Check if NOT an INT - if so, set to 0

Hi,


1.
Is there a way that I can check if the value is an INT and if its not set it to ‘0’.  Can I do this in one line of code?

Something like .. (if NOT INT (row.Cells["Reject01"].Value),0)



2. I have a datagrid.  I would only like to loop through the rows that have an entry against them, see image:  User generated image
Avatar of ste5an
ste5an
Flag of Germany image

Test for NULL, DbNull or empty.
Not one line but two lines (untested and off the top of my head):
int temp;
row.Cells["Reject01"].Value = row.Cells["Reject01"].Value != null && !string.IsNullOrEmpty(row.Cells["Reject01"].Value) ? int.TryParse(row.Cells["Reject01"].Value, out temp) ? temp.ToString() : "0" : "";

Open in new window

Which written verbose means:
int temp;
string result;
if (row.Cells["Reject01"].Value != null && !string.IsNullOrEmpty(row.Cells["Reject01"].Value))
{
	if (int.TryParse(row.Cells["Reject01"].Value, out temp))
	{
		result = temp.ToString();
	}
	else
	{
		result = "0";
	}
}
else
{
	result = "";
}
row.Cells["Reject01"].Value = result;

Open in new window


IMHO, the verbose method is easier to read and debug.  However, they will both produce the same results.

-saige-
Avatar of SmashAndGrab
SmashAndGrab

ASKER

Thanks.

Is there a short method that I could fit in here:
User generated image
I just wanted to make the code easier.  perhaps a callable function?
Use parameterized commands to avoid SQL injection.
That validating part we were talking about.

I’m trying the event you said about but not having much luck.

1.       I can’t seem to get my ValidateTheEntry() function to return a 0 if the entry is either “” or NULL or string.
2.      I only need to validate certain columns in the datagrid, I don’t need to validate the EAN or the description for instance and yet the column index don’t seem to be logical?



private void dgCleanseData_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            //validating cell
            //should only validate certain cells i.e. not the description
            if (e.ColumnIndex == 3 || e.ColumnIndex == 7)
            {
                ValidateTheEntry(e.FormattedValue.ToString());
            }
        }




public int ValidateTheEntry(string number)
        {
            int temp;
            string result;
            if (number != null && !string.IsNullOrEmpty(number))
            {
                if (int.TryParse(number, out temp))
                {
                    result = temp.ToString();
                }
                else
                {
                    result = "0";
                }
            }
            else
            {
                result = "";
            }
            number = result;
            return Convert.ToInt32(result);
        }
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial