Solved

C# -- "foreach" on first ROW ?

Posted on 2011-03-24
6
709 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

805 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