Solved

C# -- "foreach" on first ROW ?

Posted on 2011-03-24
6
706 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
Comment Utility
// 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
Comment Utility
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
Comment Utility
Hi!

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

Open in new window

0
 

Author Comment

by:finance_teacher
Comment Utility
I will test.
0
 

Expert Comment

by:ajayvegesna02
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now