开发者

Convert DataGridViewRow to String

I have a DataGridViewRow and I want to convert it to a string in the format mentioned below:

cell[0].ToString()+"\t"+cell[1].Tostring()+"\t"+...+cel开发者_开发知识库l[n].ToString()

In fact I want a string which has the string values of each cell in DataGridViewRow and a \t between them.

what is the best clean and readable way? Is using loop and checking the condition is the only way? the first solution would be to add each cell with \t to a temp string and then remove the last character of the temp string.


Whenever it's loops, LINQ comes to the rescue.

string.Join("\t", cell.Select(c => c.ToString()).ToArray())


You can extend the DataGridViewRow class:

    public static string Format(this DataGridViewRow row, string separator)
    {
        string[] values = new string[row.Cells.Count];
        for (int i = 0; i < row.Cells.Count; i++)
            values[i] = row.Cells[i].Value + "";
        return string.Join(separator, values);
    }

Then:

string msg = row.Format("\t");


string output = row.Cells.Aggregate("", (a, current) => a + current + "\t");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜