Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
This is my code

C#
e.Row.Cells[1].Text = e.Row.Cells[1].Text +","+ e.Row.Cells[2].Text + "," + e.Row.Cells[3].Text;


here am merging three cells on gridview .
but am getting problem with comma if any cell is empty then also comma is printing

like
us,uk,(correct)
us, ,(like am getting)

Please give me solution for this problem.
Posted
v2

Somthing like this might do the job:

C#
private string CellCombination(IEnumerable<string> values)
{
	string result = "";
	foreach (string s in values)
	{
		if (!string.IsNullOrWhiteSpace(s)) result += string.Format("{0},", s);
	}

	return result.Trim(',');

}</string>


Or the same thing using Linq

C#
private string CellCombination(IEnumerable<string> values)
{
    string result = values.Where(s => !string.IsNullOrWhiteSpace(s)).Aggregate("", (current, s) => current + string.Format("{0},", s));

    return result.Trim(',');

 }


To use it, just pass in your cells into the function as an enumerable collection.

So something like this:

e.Row.Cells[1].Text = CellCombination(new [] {e.Row.Cells[1].Text, e.Row.Cells[2].Text, e.Row.Cells[3].Text });
 
Share this answer
 
v3
Comments
[no name] 8-May-13 7:07am    
Note that "IsNullOrWhiteSpace" is framework version dependent.
Pheonyx 8-May-13 7:10am    
## Wrong ##Yep, that it is, but as long as you aren't using framework less than 2 it appears to be present.
http://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx ##

My mistake, wrong link.. its Framework 4 and newer.... IsNullorEmpty is framework 2 and later.
[no name] 8-May-13 7:12am    
IsNullOrWhiteSpace and IsNullOrEmpty are completely different functions.
Pheonyx 8-May-13 7:18am    
My understanding of the difference is that IsNullOrWhiteSpace does the same as IsNullOrEmpty, but also checks for white space characters such as new lines, tabs, etc.

Explained here: http://dotsandnets.wordpress.com/2011/07/30/isnullorempty-vs-isnullorwhitespace/

So I wouldn't say they are completely different, but IsNullOrWhiteSpace is an extended version of IsNullOrEmpty.
[no name] 8-May-13 12:27pm    
Yes and? Not is the same is different is it not? Try calling IsNullOrWhiteSpace in .NET 3.5 and see how far you get :-)
C#
e.Row.Cells[1].Text = e.Row.Cells[1].Text + (string.IsNullOrEmpty(e.Row.Cells[2].Text) ? string.empty : ",") + e.Row.Cells[2].Text + (string.IsNullOrEmpty(e.Row.Cells[3].Text) ? string.empty : ",") + e.Row.Cells[3].Text;
 
Share this answer
 
v3
Comments
devchina 8-May-13 8:04am    
I am getting error cannot implicitly convert string to bool
Manoj S Rekya 8-May-13 10:49am    
I have updated the solution by including braces for the ternary operator as + operator has a higher precedence than ? operator, error is thrown.
C#
e.Row.Cells[1].Text = e.Row.Cells[1].Text + ((e.Row.Cells[2].Text.ToString().Trim().Length>0)?  ("," + e.Row.Cells[2].Text) : string.Empty )+  ((e.Row.Cells[3].Text.ToString().Trim().Length>0) ?  ("," + e.Row.Cells[3].Text) : string.Empty );

Happy Coding!
:)
 
Share this answer
 
v2
Comments
devchina 8-May-13 8:17am    
This is not working I am getting same(uk, ,)
Aarti Meswania 8-May-13 8:22am    
check updated solution
devchina 8-May-13 9:01am    
many times i was tried but it is not working

here iam tried two cells its working
if(e.Row.Cells[7].Text == " " && e.Row.Cells[8].Text !=" " )
{
e.Row.Cells[7].Text = e.Row.Cells[8].Text;
}
else if(e.Row.Cells[8].Text != " " && e.Row.Cells[7].Text !=" ")
{
e.Row.Cells[7].Text = e.Row.Cells[7].Text + "," + e.Row.Cells[8].Text;
}

But in my case three cells
Plz provide the solution
Aarti Meswania 8-May-13 9:07am    
have you try my latest solution?
devchina 8-May-13 10:13am    
only one solution is here i tried that one but it is not working

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