Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 724
  • Last Modified:

C# -- "foreach" on first ROW ?

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
finance_teacher
Asked:
finance_teacher
1 Solution
 
RGBDartCommented:
// 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
 
jdavistxCommented:
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
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Hi!

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

Open in new window

0
 
finance_teacherAuthor Commented:
I will test.
0
 
ajayvegesna02Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now