更新时间:2023-02-04 18:57:31
这样的事情可能会起到作用:
Somthing like this might do the job:
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>
或者使用Linq同样的事情
Or the same thing using Linq
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 });
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;
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!
:)