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:  Ignore the rows that do not have any entry
SmashAndGrabAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Test for NULL, DbNull or empty.
0
it_saigeDeveloperCommented:
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-
0
SmashAndGrabAuthor Commented:
Thanks.

Is there a short method that I could fit in here:
updateCode.PNG
I just wanted to make the code easier.  perhaps a callable function?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

ste5anSenior DeveloperCommented:
Use parameterized commands to avoid SQL injection.
0
SmashAndGrabAuthor Commented:
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);
        }
0
it_saigeDeveloperCommented:
Answered here: http:/Q_28642985.html#a40685630

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.