Click here to Skip to main content
15,891,706 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friend,

I have a loop in my app. Code is following-

C#
foreach (DataRow dr in dt.Rows)
{
ColorNames +=dr["ColorName"].ToString() + "\n";
}


But i don't want to add "\n" at the last datarow.
Any suggestion how to do it?

Regards
Saumitra.
Posted
Updated 10-May-12 0:56am
v2

This code may help you

C#
foreach (DataRow dr in dt.Rows)
        {
            ColorNames += ColorName + "\n";
        }
        ColorNames = ColorNames.Substring(0, (ColorNames.Length - 1));
 
Share this answer
 
Comments
CodeHawkz 10-May-12 7:28am    
Sudheer, your solution is the optimal in this kind of scenario, but you have to add a simple if clause for it to be error prone.

if (ColorNames.length > 0)
ColorNames = ColorNames.Substring(0, (ColorNames.Length - 1));

or

if (dt.Rows.Count > 0)
ColorNames = ColorNames.Substring(0, (ColorNames.Length - 1));

would do the trick :)

Hope this helps
Sudheer Nimmagadda 10-May-12 9:43am    
PasanRatnayake, your right, Thanks you
Well, you know, foreach is not the only way for iterating:
C#
int i;
for (i = 0; i < dt.Rows.Count - 1; i++)
{
  DataRow dr = dt.Rows[i];
  ColorNames += dr["ColorName"].ToString() + "\n";
}
ColorNames += dt.Rows[i].dr["ColorName"];
 
Share this answer
 
v2
Comments
Saumitra Kumar Paul 10-May-12 8:10am    
Your solution is excellent. My vote is 5/5.
You have lots of answers to play with here, but nobody has addressed the fact that you may be doing lots of string concatenations here. In general, if you are going to be appending more than a couple of elements together, you should look into using a StringBuilder. What you would end up with (using CPallin's excellent suggestion) is:
C#
StringBuilder sb = new StringBuilder();
int count = dt.Rows.Count - 1;
for (int i = 0; i < count; i++)
{
  sb.AppendFormat("{0}\n", dt.Rows[i]["ColorName"].ToString());
}
sb.Append(dt.Rows[count]["ColorName"].ToString());
ColorNames = sb.ToString();
Another thing to consider is whether you should be using \n or Environment.NewLine. As I'm not 100% sure what your requirements are there, I'll leave it to you to investigate which is best suited to your needs.
 
Share this answer
 
v2
Comments
Andy411 10-May-12 8:20am    
My 5 because of the IMO important StringBuilder. Too many developers miss that point.
CPallini 10-May-12 8:29am    
Good point.
return string.Join("\n", dr.Rows.Select(row => row["ColorName"].ToString()).ToArray());

Something like that should work, if you're using a version where System.Linq extensions are available.
 
Share this answer
 
What about:
C#
foreach (DataRow dr in dt.Rows)
{
  if(dt.Row.IndexOf(dr) < dt.Rows.Count-1)
  ColorNames +=dr["ColorName"].ToString() + "\n";
}
 
Share this answer
 
Comments
Pete O'Hanlon 10-May-12 8:06am    
Two things. First, you are doing a lot of conditional checks in here as you go through each loop iteration. Second, you haven't got the last row.
Zoltán Zörgő 10-May-12 8:09am    
"Exclude last element in a loop" means for me to exclude last row. Or not?
Pete O'Hanlon 10-May-12 8:45am    
If you read his question it states he only wants to exclude the last \n.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900