Solved

C# -- "foreach" on first ROW ?

Posted on 2011-03-24
6
712 Views
Last Modified: 2013-12-17
The below "foreach" SECTION works.

How can I make the below "for 1st" SECTION
add a . to anything that does not have a . ?
-----------------------------------------------------------
Basically I want the first line
below to be "1,2." instead of "1,2"

NAME,SSN,CHECKVIEWPAYDATE,CHECKVIEWDEDCODE,CHECKVIEWDEDAMT
"John Doe",1864,3/25/2011,1,2
"Jane Doe",1864,3/25/2011,1,2.88
-----------------------------------------------------------
        public bool ImportFileDeductions()
        {

            DataSet ds = ReadFile(filePath);

            foreach (DataRow row in ds.Tables[0].Rows)
                if (row[4].ToString() == "")
                    row[4] = 0.0;

            for 1st (DataRow row in ds.Tables[0].Rows)
                if (row[4].ToString() does not have a .)
                    then add a .

        }
0
Comment
Question by:finance_teacher
6 Comments
 
LVL 4

Expert Comment

by:RGBDart
ID: 35207729
// Rows[0] is the first row
if ds.Tables[0].Rows[0][4].ToString().Contains(".")
{
  ds.Tables[0].Rows[0][4] = ds.Tables[0].Rows[0][4].ToString() + ".";
}

Open in new window

[telepathy mode = on]
// May be you just need to add "." to every row (at 4th column), that doesn't contains "." ?
foreach (DataRow row in ds.Tables[0].Rows)
  if (!row[4].ToString().Contains("."))
     row[4] = row[4].ToString()+".";

Open in new window

[telepathy mode = off]
0
 
LVL 7

Accepted Solution

by:
jdavistx earned 500 total points
ID: 35210054
Not entirely sure what you're trying to do, but maybe something like this:

public bool ImportFileDeductions()
{
	try
	{
		DataSet ds = ReadFile(filePath);
		if(ds.Tables[0].Rows > 0)
		{
			foreach (var row in ds.Tables[0].Rows)
				if (String.IsNullOrEmpty(row[4].ToString())) row[4] = 0.0;
			
			if(!ds.Tables[0].Rows[0][4].ToString().EndsWith("."))
				ds.Tables[0].Rows[0][4] += ".";
		}
	}
	catch(Exception e){ MessageBox.Show(String.Format("Unable to import file deductions!\n\n{0}", e.ToString()), "Error!", MessageBoxButtons.OK, MessageBoxIcons.Error );}
}

Open in new window


You may want to consider your datasource, and the types of values you're expecting.  If you're wanting to format your output to be a uniform amount of decimal places that are much more elegant and efficient solutions than this, such as the formatting methods of String.Format(), or the overloads of .ToString()
0
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 35255305
Hi!

You should try this:
                if (!row[4].ToString().EndsWith("."))
                  row[4] = row[4] + ".";

Open in new window

0
 

Author Comment

by:finance_teacher
ID: 35391858
I will test.
0
 

Expert Comment

by:ajayvegesna02
ID: 35708506
I prefer this code commented by Expert 35210054....

But u should u break condition , so that it will come out of foreach.. so that u can restrict unneccessary loop execution....
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question