Solved

C# -- "foreach" on first ROW ?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Query not working in client's PC 1 36
ASP.NET 5 Templates 2 68
C# Single Form 8 29
I need help making a collection from a class in VB.Net 3 27
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

862 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

23 Experts available now in Live!

Get 1:1 Help Now